Following Brian M. Hunt suggestion, I check the dirty factors using the following code. In this case, I check a property called status
:
class Blog(ndb.Model):
status = ndb.IntegerProperty(default=0)
def __init__(self, *args, **kwargs):
# make sure the __old_status__ exists when you `new' an object
# instead of getting it from DataStore. e.g. b = Blog(status=1)
super(Blog, self).__init__(*args, **kwargs)
self.__old_status__ = self.status # I check the status only
@classmethod
def _post_get_hook(cls, key, future):
obj = future.get_result()
setattr(obj, '__old_status__', obj.status)
def _pre_put_hook(self):
if (not self.key.id() or self.__old_status__ != self.status) and (self.status == 1):
# the blog is (either a new blog or an existing blog) AND its status is changed to published (e.g. value = 1)
# do sth e.g. save the time when it is published, update FB object cache and etc.
pass