开发者

Python SqlAlchemy Pass Query to View Serialization Issue

What is th开发者_C百科e best way to pass a sqlalchemy query's result to the view?

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. 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.

Serializer/Deserializer objects for usage with SQLAlchemy query structures, allowing “contextual” deserialization.

Any SQLAlchemy query structure, either based on sqlalchemy.sql.* or sqlalchemy.orm.* can be used. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜