Im trying to send a the 10 latest post from an datastore in app engine.
This is the database:
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)
And this is the code:
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))
And the error-message:
TypeError: datetime.datetime(2015, 3, 21, 15, 43, 58, 248650) is not JSON serializable
Why cant JSON return the date on that format? And how do i re-format it to the correct JSON format?
Br