3

是否可以在 a 中计算出实体的脏因素pre_put_hook

我想根据实际情况有条件地执行一些回调put。例如,如果一个实体的特定属性发生了变化,我想发送一个邮件通知。我可以在调用之前手动执行此操作,put()但如果方法失败,则put()也不会调用。

4

3 回答 3

2

按照Brian M. Hunt 的建议,我使用以下代码检查脏因素。在这种情况下,我检查了一个名为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
于 2015-11-30T02:42:26.137 回答
1

Sorry, this is not an NDB feature.

于 2013-05-08T14:47:36.080 回答
1

您可以通过在ndb.Modela_post_get_hook和 a_pre_put_hook上定义分别保存属性的原始值并将放置的值与原始值进行比较来做到这一点。

于 2015-06-25T14:32:02.360 回答