I am leveraging ndb\'s to_dict method to convert an object\'s properties into a python dict. From everything I can tell, this method does not include the object\'s key or parent
Another easy way to achieve that (without having to override to_dict
) is to add a ComputedProperty
that returns the id, like so:
class MyModel(ndb.Model):
# this property always returns the value of self.key.id()
uid = ndb.ComputedProperty(lambda self: self.key.id(), indexed=False)
# other properties ...
A ComputedProperty
will be added to the result of to_dict
just like any other property.
There are just two constraints:
key
(since that would conflict with the actual key) and id
doesn't work either.Also, the computed value will be written to the data store when you call put()
, so it consumes some of your storage space.
An advantage is that this supports the include
and exclude
arguments of to_dict()
out of the box.