【发布时间】:2013-12-15 16:05:44
【问题描述】:
如何从模型(datastore ndb.Model)内部设置模型属性的值?
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()
我正在寻找的是,当运行 create new note calculate_depth 方法时,它会设置 depth 属性的值
解决方案:)
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()
【问题讨论】:
-
我正在寻找的是 ComputedProperty,感谢@Jimmy Kane 我得到了它
标签: google-app-engine python-2.7 google-cloud-datastore