推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
honmaple
V2EX  ›  Python

分享一段 sqlalchemy 序列化代码

  •  
  •   honmaple · Dec 13, 2016 · 3505 views
    This topic created in 3449 days ago, the information mentioned may be changed or developed.

    地址在这

    具体使用

    使用上很简单(以 flask-sqlalchemy 为例),原生sqlalchemy类似

    多个实例

    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'])
    

    only 字段

    serializer = Seralizer(post,include=['title'])
    

    关系查询深度

    serializer = Seralizer(post,depth=3)
    
    • depth 默认为 2
    Supplement 1  ·  Dec 15, 2016

    我的坑 Seralizer ---> Serializer

    4 replies    2016-12-15 17:51:17 +08:00
    doublepeng
        1
    doublepeng  
       Dec 14, 2016
    是否考虑非外键关联的实现呢,个人在开发中基本不使用外键。
    uxlsl
        2
    uxlsl  
       Dec 15, 2016
    请问, Seralizer 是从哪里导入?
    honmaple
        3
    honmaple  
    OP
       Dec 15, 2016
    应该是 Serializer
    honmaple
        4
    honmaple  
    OP
       Dec 15, 2016
    @doublepeng 非外键关联由于实际还未用到,暂时没考虑,不过可以去掉外键相关,并且传入相应的参数
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   931 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 19:09 · PVG 03:09 · LAX 12:09 · JFK 15:09
    ♥ Do have faith in what you're doing.