How can I serialize my model. I can serialize when the key property is not repeated.
The model looks like:
class Properties(ndb.Model):
propertyID = ndb.StringProperty(required=True)
propertyParentKey = ndb.KeyProperty()
propertyItems = ndb.KeyProperty(repeated=True)
I want to do something like
#get all in list
fetched = model.Properties.query().fetch()
#to a list of dicts
toSend = [p.to_dict() for p in fetched]
#Serialize
json.dumps(stuff=toSend)
Is it possible to serialize the model somehow? How can I deal with the list of keyproperties?
Why not build your own json friendly to dictionary method? Something like so would probably suffice:
def custom_to_dict(self):
return {
'propertyId': self.propertyID,
'propertyParentKey': self.propertyParentKey.urlsafe(),
'propertyItems': [key.urlsafe() for key in self.propertyItems]
}
https://developers.google.com/appengine/docs/python/ndb/keyclass#Key_urlsafe
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.