我不清楚如何做到这一点。
我有一个模型被定义为
class Post(ndb.Model):
author_key = ndb.KeyProperty(kind=Author)
content = ndb.StringProperty(indexed=False)
created = ndb.DateTimeProperty(auto_now_add=True)
title = ndb.StringProperty(indexed=True)
topics = ndb.StructuredProperty(Concept, repeated=True)
concise_topics = ndb.ComputedProperty(get_important_topics())
@classmethod
def get_important_topics(cls):
cls.concise_topics = filter(lambda x: x.occurrence > 2, cls.topics)
return cls.concise_topics我喜欢将concise_topics的值(与主题的类型相同)设置为通过get_important_topics方法获得的子集。一旦设置了主题属性,就应该发生这种情况。
如何在Post类中定义"concise_topics“属性?
发布于 2016-09-19 10:24:39
使用类方法,您无法访问实例值。而且您也不应该调用函数,只有将传递给计算属性,并让它自己调用。
class Post(ndb.Model):
author_key = ndb.KeyProperty(kind=Author)
content = ndb.StringProperty(indexed=False)
created = ndb.DateTimeProperty(auto_now_add=True)
title = ndb.StringProperty(indexed=True)
topics = ndb.StructuredProperty(Concept, repeated=True)
def get_important_topics(self):
return filter(lambda x: x.occurrence > 2, self.topics)
concise_topics = ndb.ComputedProperty(get_important_topics)据我所知,计算属性是在每个put调用上设置的,因此到那时您的主题应该已经在那里了。
https://stackoverflow.com/questions/39570537
复制相似问题