GitHub地址: https://github.com/honmaple/maple-jsongit
靈感來源於 Django REST frameworkgithub
posts = Post.query.all() serializer = Seralizer(posts,many=True) data = serializer.data
post = Post.query.first() serializer = Seralizer(post,many=False) data = serializer.data
serializer = Seralizer(post,exclude=['title'])
serializer = Seralizer(post,include=['title'])
serializer = Seralizer(post,depth=3)
depthsql
默認爲2
serializer = Serializer(post,extra=['get_post_count'])
Postdjango
class Post(Model): ...... def get_post_count(self): return 11
class PostSerializer(Serializer): count = Field(source = 'get_post_count',args={'name':'hello'},default=20) class Meta: include = [] depth = 2 include = [] exclude = [] extra = ['count']
djang orm與sqlalchemy相比,爲何不少人都認爲django orm更好用,大概就是由於django orm更方便json
gtflask
lt函數
ltepost
gtecode
containsorm
in
exact
iexact
startswith
istartswith
iendswith
endswith
isnull
range
year
month
day
示例:
Post.query.filter_by(title__contains = 'sql').all() Post.query.exclude_by(title__contains = 'sql').all()
Post.query.filter_by(tags__name__contains = 'sql').all()
Post.query.filter_by(tags__name__contains = 'sql').or(Post.id == 1,Post.id == 2).all() Post.query.filter_by(tags__name__contains = 'sql').and(Post.id == 1,Post.id == 2).all() Post.query.filter_by(tags__name__contains = 'sql').exists() Post.query.load_only('title')
以flask-sqlalchemy爲例,經過繼承 models.py 中的Mixin,就能夠去除部分重複工做
自增ID – id
post = Post(·····) post.save() # 保存 post.delete() # 保存
批量操做
bulk_insert
bulk_update
bulk_save
增長兩字段
created_at
數據建立時間
updated_at
數據更新時間
關聯用戶表,與User表現爲多對一關係(即一個用戶有多個post)
class Post(ModelUserMixin, Model): user_related_name = 'posts' titile = ...