#20870 closed New feature (fixed)
django.utils.functional.cached_property should be documented
Reported by: | Daniele Procida | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 1.5 |
Severity: | Normal | Keywords: | |
Cc: | info@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
It's a very common pattern in Django to find oneself calling a method in a view to check something - if MyModel.mymethod():
- then a moment later in the template {% for things in mymodel.mymethod %}
- so mymethod()
gets called twice.
If mymethod()` is very expensive, this could be a problem.
django.utils.functional.cached_property
already exists and solves the problem. Doesn't it deserve to be documented so that we can all enjoy it?
https://github.com/django/django/blob/master/django/utils/functional.py#L38
Change History (11)
comment:1 by , 11 years ago
Cc: | added |
---|
comment:2 by , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Type: | Uncategorized → New feature |
comment:3 by , 11 years ago
It doesn't seem to be impossible:
from django.utils.functional import cached_property class Foobar(object): i = 0 @cached_property def foo(self): Foobar.i += 1 return Foobar.i f = Foobar() print f.foo print f.foo del f.foo print f.foo print f.foo OUT: 1 1 2 2
If we want to document that is a different thing. That forces the implementation to have the cached value in self.__dict__
, but on the other hand that seems to be the only way to implement cached_property...
comment:4 by , 11 years ago
https://github.com/django/django/pull/1449 (closed; left here for reference only)
New pull request: https://github.com/django/django/pull/1452
comment:5 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:8 by , 11 years ago
https://github.com/django/django/pull/1452 was accepted before I had a chance to add https://github.com/django/django/pull/1455
django-developers thread asking if OK to document this has received a couple +1's with a caveat from Aymeric: "The main drawback of the current implementation is that it's impossible to clear the cached value. That should be mentioned in the docs."