如何從模型(datastore ndb.Model)中設置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()
我正在尋找的是,當創建新的註釋運行calculate_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凱恩我得到了它 –