How to serialize SqlAlchemy result to JSON?

后端 未结 27 1878
说谎
说谎 2020-11-22 09:59

Django has some good automatic serialization of ORM models returned from DB to JSON format.

How to serialize SQLAlchemy query result to JSON format?

I tried

27条回答
  •  悲&欢浪女
    2020-11-22 10:28

    While the original question goes back awhile, the number of answers here (and my own experiences) suggest it's a non-trivial question with a lot of different approaches of varying complexity with different trade-offs.

    That's why I built the SQLAthanor library that extends SQLAlchemy's declarative ORM with configurable serialization/de-serialization support that you might want to take a look at.

    The library supports:

    • Python 2.7, 3.4, 3.5, and 3.6.
    • SQLAlchemy versions 0.9 and higher
    • serialization/de-serialization to/from JSON, CSV, YAML, and Python dict
    • serialization/de-serialization of columns/attributes, relationships, hybrid properties, and association proxies
    • enabling and disabling of serialization for particular formats and columns/relationships/attributes (e.g. you want to support an inbound password value, but never include an outbound one)
    • pre-serialization and post-deserialization value processing (for validation or type coercion)
    • a pretty straightforward syntax that is both Pythonic and seamlessly consistent with SQLAlchemy's own approach

    You can check out the (I hope!) comprehensive docs here: https://sqlathanor.readthedocs.io/en/latest

    Hope this helps!

提交回复
热议问题