How to serialize SqlAlchemy result to JSON?

后端 未结 27 1523
说谎
说谎 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:11

    You can convert a RowProxy to a dict like this:

     d = dict(row.items())
    

    Then serialize that to JSON ( you will have to specify an encoder for things like datetime values ) It's not that hard if you just want one record ( and not a full hierarchy of related records ).

    json.dumps([(dict(row.items())) for row in rs])
    

提交回复
热议问题