Google app engine 在应用程序引擎模型类中自动设置属性值

Google app engine 在应用程序引擎模型类中自动设置属性值,google-app-engine,python-2.7,google-cloud-datastore,Google App Engine,Python 2.7,Google Cloud Datastore,如何从模型(datastore ndb.Model)内部设置模型属性的值 我要寻找的是,当运行createnewnotecoculate_depth方法时,它设置depth属性的值 解决方案:) 为普雷维乌斯的事道歉 你要找的是一封信 我看到在函数\u calculate\u depth中使用实体键的值。然后,不仅需要创建实体,还需要将其写入ndb中,以设置密钥 如果你想让这种情况“自动”发生,那么Model_uuPost_uPut_uHook就是你所需要的@KAl Mazrouai然后请检查这

如何从模型(datastore ndb.Model)内部设置模型属性的值

我要寻找的是,当运行createnewnotecoculate_depth方法时,它设置depth属性的值

解决方案:)


为普雷维乌斯的事道歉

你要找的是一封信

我看到在函数
\u calculate\u depth
中使用实体键的值。然后,不仅需要创建实体,还需要将其写入ndb中,以设置密钥


如果你想让这种情况“自动”发生,那么Model_uuPost_uPut_uHook就是你所需要的

@KAl Mazrouai然后请检查这个答案的正确性,欢迎使用stackoverflow。我认为前一个答案也很有效,这将有助于其他人同时保留这两个答案solutions@KAl-Mazrouai很高兴听到这个消息。appengine还有一些更好的功能。还要查找结构化属性我要查找的是ComputedProperty,多亏了@Jimmy Kane,我找到了它
class Note(ndb.Model):
    content = ndb.StringProperty()
    date = ndb.DateTimeProperty(auto_now_add=True)
    depth = ndb.IntegerProperty()

    def _calculate_depth(self):
        self.depth = len(self.key.parent().pairs())

note = Note( parent=ndb.Key('Note', 'main', 'Todo', 'task1') , content='whatever')
note.put()
class Note(ndb.Model):
    content = ndb.StringProperty()
    date = ndb.DateTimeProperty(auto_now_add=True)
    depth = ndb.ComputedProperty(lambda self: len(self.key.parent().pairs()))


note = Note( parent=ndb.Key('Note', 'main', 'Todo', 'task1') , content='whatever')
note.put()