[英]Python SqlAlchemy Pass Query to View Serialization Issue
What is the best way to pass a sqlalchemy query's result to the view? 将sqlalchemy查询结果传递给视图的最佳方法是什么?
I have a declaratively declared table such as: 我有一个声明式声明的表,例如:
class Greeting(Base):
__tablename__ = 'greetings'
id = Column(Integer, primary_key=True)
author = Column(String)
content = Column(Text)
date = Column(DateTime)
def __init__(self, author, content, date = datetime.datetime.now()):
self.author = author
self.content = content
self.date = date
Then, I run a query with q = session.query(Greeting).order_by(Greeting.date)
, but when I try to simply return q
, it throws some JSON serialization error. 然后,我使用
q = session.query(Greeting).order_by(Greeting.date)
运行查询,但是当我尝试简单地返回q
,它将引发一些JSON序列化错误。 From what I understand, this is due to the date field. 据我了解,这是由于日期字段。 Is there any simple way to fix this?
有没有简单的方法可以解决此问题?
Take a look at http://www.sqlalchemy.org/docs/core/serializer.html . 看看http://www.sqlalchemy.org/docs/core/serializer.html 。
Serializer/Deserializer objects for usage with SQLAlchemy query structures, allowing “contextual” deserialization.
用于SQLAlchemy查询结构的Serializer / Deserializer对象,允许“上下文”反序列化。
Any SQLAlchemy query structure, either based on sqlalchemy.sql.* or sqlalchemy.orm.* can be used.
可以使用基于sqlalchemy.sql。*或sqlalchemy.orm。*的任何SQLAlchemy查询结构。 The mappers, Tables, Columns, Session etc. which are referenced by the structure are not persisted in serialized form, but are instead re-associated with the query structure when it is deserialized.
结构引用的映射器,表,列,会话等不以序列化形式保留,而是在反序列化时与查询结构重新关联。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.