摘要:地址序列化為靈感來源于多個(gè)實(shí)例單個(gè)實(shí)例排除字段僅包括字段關(guān)系查詢深度默認(rèn)為增加一些自定義的函數(shù)可傳遞參數(shù)的函數(shù)像一樣使用與相比為什么很多人都認(rèn)為更好用大概就是因?yàn)楦奖慊静樵円褜?shí)現(xiàn)示例關(guān)系查詢其它去掉一些的重復(fù)工作以為例通過繼承中的就
GitHub地址: https://github.com/honmaple/maple-json
sqlalchemy object序列化為json靈感來源于 Django REST framework
多個(gè)實(shí)例posts = Post.query.all() serializer = Seralizer(posts,many=True) data = serializer.data單個(gè)實(shí)例
post = Post.query.first() serializer = Seralizer(post,many=False) data = serializer.data排除字段
serializer = Seralizer(post,exclude=["title"])僅包括字段
serializer = Seralizer(post,include=["title"])關(guān)系查詢深度
serializer = Seralizer(post,depth=3)
depth
默認(rèn)為2增加一些自定義的函數(shù)
serializer = Serializer(post,extra=["get_post_count"])
Post
class Post(Model): ...... def get_post_count(self): return 11可傳遞參數(shù)的函數(shù)
class PostSerializer(Serializer): count = Field(source = "get_post_count",args={"name":"hello"},default=20) class Meta: include = [] depth = 2 include = [] exclude = [] extra = ["count"]像django orm一樣使用sqlalchemy
djang orm與sqlalchemy相比,為什么很多人都認(rèn)為django orm更好用,大概就是因?yàn)閐jango orm更方便
基本查詢(已實(shí)現(xiàn))gt
lt
lte
gte
contains
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()關(guān)系查詢
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")去掉一些sqlalchemy的重復(fù)工作
以flask-sqlalchemy為例,通過繼承 models.py 中的Mixin,就可以去除部分重復(fù)工作
ModelMixin自增ID – id
post = Post(·····) post.save() # 保存 post.delete() # 保存
批量操作
bulk_insert
bulk_update
bulk_save
ModelTimeMixin增加兩字段
created_at
數(shù)據(jù)創(chuàng)建時(shí)間
updated_at
數(shù)據(jù)更新時(shí)間ModelUserMixin
關(guān)聯(lián)用戶表,與User表現(xiàn)為多對(duì)一關(guān)系(即一個(gè)用戶有多個(gè)post)
class Post(ModelUserMixin, Model): user_related_name = "posts" titile = ...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/38522.html
摘要:查詢記錄在調(diào)試或測試模式自動(dòng)啟用??梢杂糜陲@式禁用原生支持。當(dāng)使用不合適的指定無編碼的數(shù)據(jù)庫默認(rèn)值時(shí),這對(duì)于一些數(shù)據(jù)庫適配器是必須的比如上某些版本的。這對(duì)是必要的,它默認(rèn)移除閑置多于小時(shí)的連接。注意如果使用了,自動(dòng)設(shè)定這個(gè)值為小時(shí)。 flask-sqlalchemy是flask的一個(gè)ORM擴(kuò)展框架,這個(gè)擴(kuò)展在sqlalchemy的進(jìn)行的擴(kuò)展,更方便的結(jié)合Flask.什么是ORM?其是O...
摘要:首先,使用連接數(shù)據(jù)庫并定義表結(jié)構(gòu)初始化。創(chuàng)建新對(duì)象添加到批量添加子非魚虞姬花木蘭提交即保存到數(shù)據(jù)庫結(jié)果查的函數(shù)會(huì)返回一個(gè)對(duì)象。一對(duì)多在的一對(duì)多關(guān)系中,使用來表示表的外鍵,表示表與表之間關(guān)聯(lián)的屬性。 Python支持的數(shù)據(jù)庫有很多,MySQL作為主流數(shù)據(jù)庫之一,我們不妨了解下它們之間的小故事Python操作MySQL的庫有三個(gè),python-MySQL(MySQLdb),PyMySQL跟...
摘要:首先,使用連接數(shù)據(jù)庫并定義表結(jié)構(gòu)初始化。創(chuàng)建新對(duì)象添加到批量添加子非魚虞姬花木蘭提交即保存到數(shù)據(jù)庫結(jié)果查的函數(shù)會(huì)返回一個(gè)對(duì)象。一對(duì)多在的一對(duì)多關(guān)系中,使用來表示表的外鍵,表示表與表之間關(guān)聯(lián)的屬性。 Python支持的數(shù)據(jù)庫有很多,MySQL作為主流數(shù)據(jù)庫之一,我們不妨了解下它們之間的小故事Python操作MySQL的庫有三個(gè),python-MySQL(MySQLdb),PyMySQL跟...
閱讀 5304·2021-09-22 15:50
閱讀 1884·2021-09-02 15:15
閱讀 1181·2019-08-29 12:49
閱讀 2557·2019-08-26 13:31
閱讀 3476·2019-08-26 12:09
閱讀 1224·2019-08-23 18:17
閱讀 2755·2019-08-23 17:56
閱讀 2945·2019-08-23 16:02