Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.6.x] Fixed #20870 -- Documented django.utils.functional.cached_pro…

…perty

Backport of 7a2296e from master
  • Loading branch information...
commit 25ce1e0e0a37d7be7ceff1e37bcb9ebd78a959d9 1 parent 6264e39
Daniele Procida evildmp authored timgraham committed
Showing with 44 additions and 0 deletions.
  1. +44 −0 docs/ref/utils.txt
44 docs/ref/utils.txt
View
@@ -447,6 +447,50 @@ Atom1Feed
.. module:: django.utils.functional
:synopsis: Functional programming tools.
+.. class:: cached_property(object)
+
+ The ``@cached_property`` decorator caches the result of a method with a
+ single ``self`` argument as a property. The cached result will persist as
+ long as the instance does.
+
+ Consider a typical case, where a view might need to call a model's method
+ to perform some computation, before placing the model instance into the
+ context, where the template might invoke the method once more::
+
+ # the model
+ class Person(models.Model):
+
+ def friends(self):
+ # expensive computation
+ ...
+ return friends
+
+ # in the view:
+ if person.friends():
+
+ # in the template:
+ {% for friend in person.friends %}
+
+ ``friends()`` will be called twice. Since the instance ``person`` in
+ the view and the template are the same, ``@cached_property`` can avoid
+ that::
+
+ from django.utils.functional import cached_property
+
+ @cached_property
+ def friends(self):
+ # expensive computation
+ ...
+ return friends
+
+ Note that as the method is now a property, in Python code it will need to
+ be invoked appropriately::
+
+ # in the view:
+ if person.friends:
+
+ You may clear the cached result using ``del person.friends``.
+
.. function:: allow_lazy(func, *resultclasses)
Django offers many utility functions (particularly in ``django.utils``) that
Please sign in to comment.
Something went wrong with that request. Please try again.