小编use*_*168的帖子

如何在django中使@cached_property无效

大家好我现在正在模型类上使用@cached_property,并且喜欢在保存时删除它,以便在下次调用时可以重新填充它我该怎么做.例:

class Amodel():
    #...model_fields....

    @cached_property
    def db_connection(self):
        #get some thing in the db and cache here


instance = Amodel.objects.get(id=1)
variable = instance.db_connection

Amodel.objects.select_for_update().filter(id=1).update(#some variable)
#invalidate instance.db_connection
#new_variable = instance.db_connection
Run Code Online (Sandbox Code Playgroud)

谢谢

django django-models django-views

23
推荐指数
4
解决办法
8973
查看次数

标签 统计

django ×1

django-models ×1

django-views ×1