亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

sqlalchemy使用上的小tip

tyheist / 1073人閱讀

摘要:地址序列化為靈感來源于多個實(shí)例單個實(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

多個實(shí)例
posts = Post.query.all()
serializer = Seralizer(posts,many=True)
data = serializer.data

單個實(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)建時間

updated_at

數(shù)據(jù)更新時間

ModelUserMixin

關(guān)聯(lián)用戶表,與User表現(xiàn)為多對一關(guān)系(即一個用戶有多個post)

class Post(ModelUserMixin, Model):

    user_related_name = "posts"
    titile = ...

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/38522.html

相關(guān)文章

  • Flask擴(kuò)展之flask-sqlalchemy(上)

    摘要:查詢記錄在調(diào)試或測試模式自動啟用??梢杂糜陲@式禁用原生支持。當(dāng)使用不合適的指定無編碼的數(shù)據(jù)庫默認(rèn)值時,這對于一些數(shù)據(jù)庫適配器是必須的比如上某些版本的。這對是必要的,它默認(rèn)移除閑置多于小時的連接。注意如果使用了,自動設(shè)定這個值為小時。 flask-sqlalchemy是flask的一個ORM擴(kuò)展框架,這個擴(kuò)展在sqlalchemy的進(jìn)行的擴(kuò)展,更方便的結(jié)合Flask.什么是ORM?其是O...

    KaltZK 評論0 收藏0
  • Python與數(shù)據(jù)庫的新人手冊 -- MySQL

    摘要:首先,使用連接數(shù)據(jù)庫并定義表結(jié)構(gòu)初始化。創(chuàng)建新對象添加到批量添加子非魚虞姬花木蘭提交即保存到數(shù)據(jù)庫結(jié)果查的函數(shù)會返回一個對象。一對多在的一對多關(guān)系中,使用來表示表的外鍵,表示表與表之間關(guān)聯(lián)的屬性。 Python支持的數(shù)據(jù)庫有很多,MySQL作為主流數(shù)據(jù)庫之一,我們不妨了解下它們之間的小故事Python操作MySQL的庫有三個,python-MySQL(MySQLdb),PyMySQL跟...

    endiat 評論0 收藏0
  • Python與數(shù)據(jù)庫的新人手冊 -- MySQL

    摘要:首先,使用連接數(shù)據(jù)庫并定義表結(jié)構(gòu)初始化。創(chuàng)建新對象添加到批量添加子非魚虞姬花木蘭提交即保存到數(shù)據(jù)庫結(jié)果查的函數(shù)會返回一個對象。一對多在的一對多關(guān)系中,使用來表示表的外鍵,表示表與表之間關(guān)聯(lián)的屬性。 Python支持的數(shù)據(jù)庫有很多,MySQL作為主流數(shù)據(jù)庫之一,我們不妨了解下它們之間的小故事Python操作MySQL的庫有三個,python-MySQL(MySQLdb),PyMySQL跟...

    liuyix 評論0 收藏0

發(fā)表評論

0條評論

tyheist

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<