Kaydet (Commit) ec05ff08 authored tarafından François Freitag's avatar François Freitag Kaydeden (comit) Tim Graham

Refs #25546 -- Added detailed comments for prefetch_related test.

üst c0a2b950
...@@ -1265,13 +1265,19 @@ class Ticket21760Tests(TestCase): ...@@ -1265,13 +1265,19 @@ class Ticket21760Tests(TestCase):
self.assertNotIn(' JOIN ', str(queryset.query)) self.assertNotIn(' JOIN ', str(queryset.query))
class Ticket25546Tests(TestCase): class DirectPrefechedObjectCacheReuseTests(TestCase):
""" """
Nested prefetch_related() shouldn't trigger duplicate queries for the same prefetch_related() reuses objects fetched in _prefetched_objects_cache.
lookup.
Before, prefetch queries were for 'addresses', 'first_time_authors', and When objects are prefetched and not stored as an instance attribute (often
'first_time_authors__addresses'. The last query is the duplicate. intermediary relationships), they are saved to the
_prefetched_objects_cache attribute. prefetch_related() takes
_prefetched_objects_cache into account when determining whether an object
has been fetched[1] and retrieves results from it when it is populated [2].
[1]: #25546 (duplicate queries on nested Prefetch)
[2]: #27554 (queryset evaluation fails with a mix of nested and flattened
prefetches)
""" """
@classmethod @classmethod
...@@ -1291,7 +1297,11 @@ class Ticket25546Tests(TestCase): ...@@ -1291,7 +1297,11 @@ class Ticket25546Tests(TestCase):
AuthorAddress.objects.create(author=cls.author21, address='Happy place'), AuthorAddress.objects.create(author=cls.author21, address='Happy place'),
] ]
def test_prefetch(self): def test_detect_is_fetched(self):
"""
Nested prefetch_related() shouldn't trigger duplicate queries for the same
lookup.
"""
with self.assertNumQueries(3): with self.assertNumQueries(3):
books = Book.objects.filter( books = Book.objects.filter(
title__in=['book1', 'book2'], title__in=['book1', 'book2'],
...@@ -1335,7 +1345,7 @@ class Ticket25546Tests(TestCase): ...@@ -1335,7 +1345,7 @@ class Ticket25546Tests(TestCase):
list(book2.first_time_authors.all()[0].addresses.all().all()) list(book2.first_time_authors.all()[0].addresses.all().all())
) )
def test_prefetch_with_to_attr(self): def test_detect_is_fetched_with_to_attr(self):
with self.assertNumQueries(3): with self.assertNumQueries(3):
books = Book.objects.filter( books = Book.objects.filter(
title__in=['book1', 'book2'], title__in=['book1', 'book2'],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment