summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2016-07-19 14:55:59 -0400
committerSimon Charette <charette.s@gmail.com>2016-07-19 16:09:23 -0400
commitdcf0a35b088ba9ce8907d8f34178bd6dffc27022 (patch)
tree1bb10b6d6877582cbd2f5dcb3e47f68a7b1f1693 /tests
parent53d17f9e75e5abffa43c2fbed929f703d3f0e7e8 (diff)
[1.10.x] Fixed #26916 -- Fixed prefetch_related when using a cached_property as to_attr.
Thanks Trac alias karyon for the report and Tim for the review. Backport of 271bfe65d986f5ecbaeb7a70a3092356c0a9e222 from master
Diffstat (limited to 'tests')
-rw-r--r--tests/prefetch_related/models.py5
-rw-r--r--tests/prefetch_related/tests.py11
2 files changed, 16 insertions, 0 deletions
diff --git a/tests/prefetch_related/models.py b/tests/prefetch_related/models.py
index 32570e9109..064ce1dfbd 100644
--- a/tests/prefetch_related/models.py
+++ b/tests/prefetch_related/models.py
@@ -6,6 +6,7 @@ from django.contrib.contenttypes.fields import (
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
+from django.utils.functional import cached_property
# Basic tests
@@ -219,6 +220,10 @@ class Person(models.Model):
def all_houses(self):
return list(self.houses.all())
+ @cached_property
+ def cached_all_houses(self):
+ return self.all_houses
+
class Meta:
ordering = ['id']
diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py
index c34682a33d..7c36975084 100644
--- a/tests/prefetch_related/tests.py
+++ b/tests/prefetch_related/tests.py
@@ -743,6 +743,17 @@ class CustomPrefetchTests(TestCase):
).get(pk=self.house3.pk)
self.assertIsInstance(house.rooms.all(), QuerySet)
+ def test_to_attr_cached_property(self):
+ persons = Person.objects.prefetch_related(
+ Prefetch('houses', House.objects.all(), to_attr='cached_all_houses'),
+ )
+ for person in persons:
+ # To bypass caching at the related descriptor level, don't use
+ # person.houses.all() here.
+ all_houses = list(House.objects.filter(occupants=person))
+ with self.assertNumQueries(0):
+ self.assertEqual(person.cached_all_houses, all_houses)
+
class DefaultManagerTests(TestCase):