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'))
The @cached_property decorator caches the result of a method with a single self argument as a property.
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.
You can invalidate the cached_property
by deleting it:
del self.options
See here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With