mirror of
https://github.com/django/django.git
synced 2025-08-01 17:42:56 +00:00
Fixed #32089 -- Fixed prefetch_related_objects() when some objects are already fetched.
Thanks Dennis Kliban for the report and Adam Johnson for the initial patch. Co-authored-by: Adam Johnson <me@adamj.eu>
This commit is contained in:
parent
bebd4cfa8f
commit
b9ba85a7ce
2 changed files with 52 additions and 10 deletions
|
@ -97,6 +97,16 @@ class PrefetchRelatedObjectsTests(TestCase):
|
|||
with self.assertNumQueries(0):
|
||||
self.assertCountEqual(book1.authors.all(), [self.author1, self.author2, self.author3])
|
||||
|
||||
def test_prefetch_object_twice(self):
|
||||
book1 = Book.objects.get(id=self.book1.id)
|
||||
book2 = Book.objects.get(id=self.book2.id)
|
||||
with self.assertNumQueries(1):
|
||||
prefetch_related_objects([book1], Prefetch('authors'))
|
||||
with self.assertNumQueries(1):
|
||||
prefetch_related_objects([book1, book2], Prefetch('authors'))
|
||||
with self.assertNumQueries(0):
|
||||
self.assertCountEqual(book2.authors.all(), [self.author1])
|
||||
|
||||
def test_prefetch_object_to_attr(self):
|
||||
book1 = Book.objects.get(id=self.book1.id)
|
||||
with self.assertNumQueries(1):
|
||||
|
@ -105,6 +115,22 @@ class PrefetchRelatedObjectsTests(TestCase):
|
|||
with self.assertNumQueries(0):
|
||||
self.assertCountEqual(book1.the_authors, [self.author1, self.author2, self.author3])
|
||||
|
||||
def test_prefetch_object_to_attr_twice(self):
|
||||
book1 = Book.objects.get(id=self.book1.id)
|
||||
book2 = Book.objects.get(id=self.book2.id)
|
||||
with self.assertNumQueries(1):
|
||||
prefetch_related_objects(
|
||||
[book1],
|
||||
Prefetch('authors', to_attr='the_authors'),
|
||||
)
|
||||
with self.assertNumQueries(1):
|
||||
prefetch_related_objects(
|
||||
[book1, book2],
|
||||
Prefetch('authors', to_attr='the_authors'),
|
||||
)
|
||||
with self.assertNumQueries(0):
|
||||
self.assertCountEqual(book2.the_authors, [self.author1])
|
||||
|
||||
def test_prefetch_queryset(self):
|
||||
book1 = Book.objects.get(id=self.book1.id)
|
||||
with self.assertNumQueries(1):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue