Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update django cached_property

Tags:

python

django

I would like to use @cached_property on model property to avoid too much db access.

class AttrGroup(models.Model):
    ...

    @cached_property
    def options(self):
        options = AttributeOption.objects.filter(group_id=self.id)
        return options
    ...

This works fine.

i want to use the following function to update the value of options.But how should i do it? for property decorator, there is setter for it.

def _set_options(self, value):
    for option in value:
        AttributeOption.objects.create(group_id=self.id, option=option.get('option'))
like image 726
Emma Avatar asked Mar 27 '17 06:03

Emma


People also ask

What is Cached_property in Django?

The @cached_property decorator caches the result of a method with a single self argument as a property.

What is utils PY in Django?

Python Utils is a collection of small Python functions and classes which make common patterns shorter and easier. It is by no means a complete collection but it has served me quite a bit in the past and I will keep extending it. One of the libraries using Python Utils is Django Utils.


1 Answers

You can invalidate the cached_property by deleting it:

del self.options

See here.

like image 74
albar Avatar answered Oct 18 '22 14:10

albar