首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >的计算属性

的计算属性
EN

Stack Overflow用户
提问于 2016-09-19 10:09:33
回答 1查看 272关注 0票数 0

我不清楚如何做到这一点。

我有一个模型被定义为

代码语言:javascript
复制
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“属性?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-19 10:24:39

使用类方法,您无法访问实例值。而且您也不应该调用函数,只有传递给计算属性,并让它自己调用。

代码语言:javascript
复制
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调用上设置的,因此到那时您的主题应该已经在那里了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39570537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档