【发布时间】:2015-03-21 15:51:20
【问题描述】:
我正在尝试从应用引擎中的数据存储区发送 10 个最新帖子。
这是数据库:
class Author(ndb.Model):
"""Sub model for representing an author."""
identity = ndb.StringProperty(indexed=False)
email = ndb.StringProperty(indexed=True)
class Message(ndb.Model):
"""A main model for representing an individual Message entry."""
author = ndb.StructuredProperty(Author)
content = ndb.StringProperty(indexed=False)
date = ndb.DateTimeProperty(auto_now_add=True)
这是代码:
class Query(webapp2.RequestHandler):
def post(self):
message_name = self.request.get('db_name',
DEFAULT_KEY)
message_query = Message.query(ancestor=db_key(
message_name)).order(-Message.date)
messages = message_query.fetch(10)
items = []
for message in message_query:
items.append({'id': message.author.identity,
'email':message.author.email,
'content':message.content,
'date':message.date})
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(items))
还有错误信息:
TypeError: datetime.datetime(2015, 3, 21, 15, 43, 58, 248650) is not JSON serializable
为什么 JSON 不能以该格式返回日期? 以及如何将其重新格式化为正确的 JSON 格式?
Br
【问题讨论】:
标签: python json google-app-engine