1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

[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 271bfe65d9 from master
This commit is contained in:
Simon Charette
2016-07-19 14:55:59 -04:00
parent 53d17f9e75
commit dcf0a35b08
3 changed files with 23 additions and 2 deletions

View File

@@ -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']

View File

@@ -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):