My NDB data model looks like this
class Foo(ndb.Model):
created = ndb.DateTimeProperty(auto_now_add=True)
# some other properties
@property
def readable_created(self):
return readble_time(self.created)
where readable_time
returns some readable version of a DateTime (e.g. '2 days ago'). Thanks to the @property decorator, I can easily access it as myinstance.readable_created
. I would like to use NDB's Model.to_dict()
method to turn the data into a dictionary, say for writing it to a template. However, readable_created
is ignored by this method and myinstance.to_dict(include=['readable_created'])
returns {}
.
It's the first time I use the @property
decorator, so maybe I missed something?
Is there an easy way around this? Is this a bug?
The approach from ndb to_dict method does not include object's key works, but there should be an easier way.
Thanks!
This is by design. Some templating engines let you access attributes directly, so you won't need to use to_dict() at all (as Tim Hoffman pointed out).