I want to run some stuff on model creation, but not on model update. I could do this by adding a property, but I'm wondering if there's some kind of built in functionality for targeting specifically creation vs update.
Any entity that has been stored has a key, so checking for that will tell you if it's new or being updated.
https://developers.google.com/appengine/docs/python/ndb/keyclass
You need to check for the presence of an ID in the model key within the _pre_put_hook.
If you look at the source for the ndb Model class you can see that a key is actually created and assigned to the model instance before the _pre_put_hook is called, but the key has no ID. This code within the _pre_put_hook should work:
def _pre_put_hook(self):
if self.key.id() is None:
print 'model has not been saved'
else:
print 'model has been saved'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With