小编Emm*_*mma的帖子

如何更新django cached_property

我想在模型属性上使用@cached_property以避免过多的数据库访问.

class AttrGroup(models.Model):
    ...

    @cached_property
    def options(self):
        options = AttributeOption.objects.filter(group_id=self.id)
        return options
    ...
Run Code Online (Sandbox Code Playgroud)

这很好用.

我想使用以下函数来更新options的值.但我该怎么做呢?对于属性装饰器,它有一个setter.

def _set_options(self, value):
    for option in value:
        AttributeOption.objects.create(group_id=self.id, option=option.get('option'))
Run Code Online (Sandbox Code Playgroud)

python django

5
推荐指数
2
解决办法
2267
查看次数

标签 统计

django ×1

python ×1