Debian Codegraph @

tests/prefetch_related/tests.py

raw
1 from unittest import mock
2
3 from django.contrib.contenttypes.models import ContentType
4 from django.core.exceptions import ObjectDoesNotExist
5 from django.db import NotSupportedError, connection
6 from django.db.models import F, Prefetch, QuerySet, prefetch_related_objects
7 from django.db.models.fields.related import ForwardManyToOneDescriptor
8 from django.db.models.query import get_prefetcher, prefetch_one_level
9 from django.db.models.sql import Query
10 from django.test import (
11 TestCase,
12 ignore_warnings,
13 override_settings,
14 skipIfDBFeature,
15 skipUnlessDBFeature,
16 )
17 from django.test.utils import CaptureQueriesContext
18 from django.utils.deprecation import RemovedInDjango60Warning
19
20 from .models import (
21 Article,
22 Author,
23 Author2,
24 AuthorAddress,
25 AuthorWithAge,
26 Bio,
27 Book,
28 Bookmark,
29 BookReview,
30 BookWithYear,
31 Comment,
32 Department,
33 Employee,
34 FavoriteAuthors,
35 House,
36 LessonEntry,
37 ModelIterableSubclass,
38 Person,
39 Qualification,
40 Reader,
41 Room,
42 SelfDirectedEmployee,
43 TaggedItem,
44 Teacher,
45 WordEntry,
46 )
47
48
49 class TestDataMixin:
50 @classmethod
51 def setUpTestData(cls):
52 cls.book1 = Book.objects.create(title="Poems")
53 cls.book2 = Book.objects.create(title="Jane Eyre")
54 cls.book3 = Book.objects.create(title="Wuthering Heights")
55 cls.book4 = Book.objects.create(title="Sense and Sensibility")
56
57 cls.author1 = Author.objects.create(name="Charlotte", first_book=cls.book1)
58 cls.author2 = Author.objects.create(name="Anne", first_book=cls.book1)
59 cls.author3 = Author.objects.create(name="Emily", first_book=cls.book1)
60 cls.author4 = Author.objects.create(name="Jane", first_book=cls.book4)
61
62 cls.book1.authors.add(cls.author1, cls.author2, cls.author3)
63 cls.book2.authors.add(cls.author1)
64 cls.book3.authors.add(cls.author3)
65 cls.book4.authors.add(cls.author4)
66
67 cls.reader1 = Reader.objects.create(name="Amy")
68 cls.reader2 = Reader.objects.create(name="Belinda")
69
70 cls.reader1.books_read.add(cls.book1, cls.book4)
71 cls.reader2.books_read.add(cls.book2, cls.book4)
72
73
74 class PrefetchRelatedTests(TestDataMixin, TestCase):
75 def assertWhereContains(self, sql, needle):
76 where_idx = sql.index("WHERE")
77 self.assertEqual(
78 sql.count(str(needle), where_idx),
79 1,
80 msg="WHERE clause doesn't contain %s, actual SQL: %s"
81 % (needle, sql[where_idx:]),
82 )
83
84 def test_m2m_forward(self):
85 with self.assertNumQueries(2):
86 lists = [
87 list(b.authors.all()) for b in Book.objects.prefetch_related("authors")
88 ]
89
90 normal_lists = [list(b.authors.all()) for b in Book.objects.all()]
91 self.assertEqual(lists, normal_lists)
92
93 def test_m2m_reverse(self):
94 with self.assertNumQueries(2):
95 lists = [
96 list(a.books.all()) for a in Author.objects.prefetch_related("books")
97 ]
98
99 normal_lists = [list(a.books.all()) for a in Author.objects.all()]
100 self.assertEqual(lists, normal_lists)
101
102 def test_foreignkey_forward(self):
103 with self.assertNumQueries(2):
104 books = [
105 a.first_book for a in Author.objects.prefetch_related("first_book")
106 ]
107
108 normal_books = [a.first_book for a in Author.objects.all()]
109 self.assertEqual(books, normal_books)
110
111 def test_foreignkey_reverse(self):
112 with self.assertNumQueries(2):
113 [
114 list(b.first_time_authors.all())
115 for b in Book.objects.prefetch_related("first_time_authors")
116 ]
117
118 self.assertSequenceEqual(self.book2.authors.all(), [self.author1])
119
120 def test_onetoone_reverse_no_match(self):
121 # Regression for #17439
122 with self.assertNumQueries(2):
123 book = Book.objects.prefetch_related("bookwithyear").all()[0]
124 with self.assertNumQueries(0):
125 with self.assertRaises(BookWithYear.DoesNotExist):
126 book.bookwithyear
127
128 def test_onetoone_reverse_with_to_field_pk(self):
129 """
130 A model (Bio) with a OneToOneField primary key (author) that references
131 a non-pk field (name) on the related model (Author) is prefetchable.
132 """
133 Bio.objects.bulk_create(
134 [
135 Bio(author=self.author1),
136 Bio(author=self.author2),
137 Bio(author=self.author3),
138 ]
139 )
140 authors = Author.objects.filter(
141 name__in=[self.author1, self.author2, self.author3],
142 ).prefetch_related("bio")
143 with self.assertNumQueries(2):
144 for author in authors:
145 self.assertEqual(author.name, author.bio.author.name)
146
147 def test_survives_clone(self):
148 with self.assertNumQueries(2):
149 [
150 list(b.first_time_authors.all())
151 for b in Book.objects.prefetch_related("first_time_authors").exclude(
152 id=1000
153 )
154 ]
155
156 def test_len(self):
157 with self.assertNumQueries(2):
158 qs = Book.objects.prefetch_related("first_time_authors")
159 len(qs)
160 [list(b.first_time_authors.all()) for b in qs]
161
162 def test_bool(self):
163 with self.assertNumQueries(2):
164 qs = Book.objects.prefetch_related("first_time_authors")
165 bool(qs)
166 [list(b.first_time_authors.all()) for b in qs]
167
168 def test_count(self):
169 with self.assertNumQueries(2):
170 qs = Book.objects.prefetch_related("first_time_authors")
171 [b.first_time_authors.count() for b in qs]
172
173 def test_exists(self):
174 with self.assertNumQueries(2):
175 qs = Book.objects.prefetch_related("first_time_authors")
176 [b.first_time_authors.exists() for b in qs]
177
178 def test_in_and_prefetch_related(self):
179 """
180 Regression test for #20242 - QuerySet "in" didn't work the first time
181 when using prefetch_related. This was fixed by the removal of chunked
182 reads from QuerySet iteration in
183 70679243d1786e03557c28929f9762a119e3ac14.
184 """
185 qs = Book.objects.prefetch_related("first_time_authors")
186 self.assertIn(qs[0], qs)
187
188 def test_clear(self):
189 with self.assertNumQueries(5):
190 with_prefetch = Author.objects.prefetch_related("books")
191 without_prefetch = with_prefetch.prefetch_related(None)
192 [list(a.books.all()) for a in without_prefetch]
193
194 def test_m2m_then_m2m(self):
195 """A m2m can be followed through another m2m."""
196 with self.assertNumQueries(3):
197 qs = Author.objects.prefetch_related("books__read_by")
198 lists = [
199 [[str(r) for r in b.read_by.all()] for b in a.books.all()] for a in qs
200 ]
201 self.assertEqual(
202 lists,
203 [
204 [["Amy"], ["Belinda"]], # Charlotte - Poems, Jane Eyre
205 [["Amy"]], # Anne - Poems
206 [["Amy"], []], # Emily - Poems, Wuthering Heights
207 [["Amy", "Belinda"]], # Jane - Sense and Sense
208 ],
209 )
210
211 def test_overriding_prefetch(self):
212 with self.assertNumQueries(3):
213 qs = Author.objects.prefetch_related("books", "books__read_by")
214 lists = [
215 [[str(r) for r in b.read_by.all()] for b in a.books.all()] for a in qs
216 ]
217 self.assertEqual(
218 lists,
219 [
220 [["Amy"], ["Belinda"]], # Charlotte - Poems, Jane Eyre
221 [["Amy"]], # Anne - Poems
222 [["Amy"], []], # Emily - Poems, Wuthering Heights
223 [["Amy", "Belinda"]], # Jane - Sense and Sense
224 ],
225 )
226 with self.assertNumQueries(3):
227 qs = Author.objects.prefetch_related("books__read_by", "books")
228 lists = [
229 [[str(r) for r in b.read_by.all()] for b in a.books.all()] for a in qs
230 ]
231 self.assertEqual(
232 lists,
233 [
234 [["Amy"], ["Belinda"]], # Charlotte - Poems, Jane Eyre
235 [["Amy"]], # Anne - Poems
236 [["Amy"], []], # Emily - Poems, Wuthering Heights
237 [["Amy", "Belinda"]], # Jane - Sense and Sense
238 ],
239 )
240
241 def test_get(self):
242 """
243 Objects retrieved with .get() get the prefetch behavior.
244 """
245 # Need a double
246 with self.assertNumQueries(3):
247 author = Author.objects.prefetch_related("books__read_by").get(
248 name="Charlotte"
249 )
250 lists = [[str(r) for r in b.read_by.all()] for b in author.books.all()]
251 self.assertEqual(lists, [["Amy"], ["Belinda"]]) # Poems, Jane Eyre
252
253 def test_foreign_key_then_m2m(self):
254 """
255 A m2m relation can be followed after a relation like ForeignKey that
256 doesn't have many objects.
257 """
258 with self.assertNumQueries(2):
259 qs = Author.objects.select_related("first_book").prefetch_related(
260 "first_book__read_by"
261 )
262 lists = [[str(r) for r in a.first_book.read_by.all()] for a in qs]
263 self.assertEqual(lists, [["Amy"], ["Amy"], ["Amy"], ["Amy", "Belinda"]])
264
265 def test_reverse_one_to_one_then_m2m(self):
266 """
267 A m2m relation can be followed after going through the select_related
268 reverse of an o2o.
269 """
270 qs = Author.objects.prefetch_related("bio__books").select_related("bio")
271
272 with self.assertNumQueries(1):
273 list(qs.all())
274
275 Bio.objects.create(author=self.author1)
276 with self.assertNumQueries(2):
277 list(qs.all())
278
279 def test_attribute_error(self):
280 qs = Reader.objects.prefetch_related("books_read__xyz")
281 msg = (
282 "Cannot find 'xyz' on Book object, 'books_read__xyz' "
283 "is an invalid parameter to prefetch_related()"
284 )
285 with self.assertRaisesMessage(AttributeError, msg) as cm:
286 list(qs)
287
288 self.assertIn("prefetch_related", str(cm.exception))
289
290 def test_invalid_final_lookup(self):
291 qs = Book.objects.prefetch_related("authors__name")
292 msg = (
293 "'authors__name' does not resolve to an item that supports "
294 "prefetching - this is an invalid parameter to prefetch_related()."
295 )
296 with self.assertRaisesMessage(ValueError, msg) as cm:
297 list(qs)
298
299 self.assertIn("prefetch_related", str(cm.exception))
300 self.assertIn("name", str(cm.exception))
301
302 def test_prefetch_eq(self):
303 prefetch_1 = Prefetch("authors", queryset=Author.objects.all())
304 prefetch_2 = Prefetch("books", queryset=Book.objects.all())
305 self.assertEqual(prefetch_1, prefetch_1)
306 self.assertEqual(prefetch_1, mock.ANY)
307 self.assertNotEqual(prefetch_1, prefetch_2)
308
309 def test_forward_m2m_to_attr_conflict(self):
310 msg = "to_attr=authors conflicts with a field on the Book model."
311 authors = Author.objects.all()
312 with self.assertRaisesMessage(ValueError, msg):
313 list(
314 Book.objects.prefetch_related(
315 Prefetch("authors", queryset=authors, to_attr="authors"),
316 )
317 )
318 # Without the ValueError, an author was deleted due to the implicit
319 # save of the relation assignment.
320 self.assertEqual(self.book1.authors.count(), 3)
321
322 def test_reverse_m2m_to_attr_conflict(self):
323 msg = "to_attr=books conflicts with a field on the Author model."
324 poems = Book.objects.filter(title="Poems")
325 with self.assertRaisesMessage(ValueError, msg):
326 list(
327 Author.objects.prefetch_related(
328 Prefetch("books", queryset=poems, to_attr="books"),
329 )
330 )
331 # Without the ValueError, a book was deleted due to the implicit
332 # save of reverse relation assignment.
333 self.assertEqual(self.author1.books.count(), 2)
334
335 def test_m2m_then_reverse_fk_object_ids(self):
336 with CaptureQueriesContext(connection) as queries:
337 list(Book.objects.prefetch_related("authors__addresses"))
338
339 sql = queries[-1]["sql"]
340 self.assertWhereContains(sql, self.author1.name)
341
342 def test_m2m_then_m2m_object_ids(self):
343 with CaptureQueriesContext(connection) as queries:
344 list(Book.objects.prefetch_related("authors__favorite_authors"))
345
346 sql = queries[-1]["sql"]
347 self.assertWhereContains(sql, self.author1.name)
348
349 def test_m2m_then_reverse_one_to_one_object_ids(self):
350 with CaptureQueriesContext(connection) as queries:
351 list(Book.objects.prefetch_related("authors__authorwithage"))
352
353 sql = queries[-1]["sql"]
354 self.assertWhereContains(sql, self.author1.id)
355
356 def test_filter_deferred(self):
357 """
358 Related filtering of prefetched querysets is deferred on m2m and
359 reverse m2o relations until necessary.
360 """
361 add_q = Query.add_q
362 for relation in ["authors", "first_time_authors"]:
363 with self.subTest(relation=relation):
364 with mock.patch.object(
365 Query,
366 "add_q",
367 autospec=True,
368 side_effect=lambda self, q, reuse_all: add_q(self, q),
369 ) as add_q_mock:
370 list(Book.objects.prefetch_related(relation))
371 self.assertEqual(add_q_mock.call_count, 1)
372
373 def test_named_values_list(self):
374 qs = Author.objects.prefetch_related("books")
375 self.assertCountEqual(
376 [value.name for value in qs.values_list("name", named=True)],
377 ["Anne", "Charlotte", "Emily", "Jane"],
378 )
379
380 def test_m2m_prefetching_iterator_with_chunks(self):
381 with self.assertNumQueries(3):
382 authors = [
383 b.authors.first()
384 for b in Book.objects.prefetch_related("authors").iterator(chunk_size=2)
385 ]
386 self.assertEqual(
387 authors,
388 [self.author1, self.author1, self.author3, self.author4],
389 )
390
391 def test_m2m_prefetching_iterator_without_chunks_error(self):
392 msg = (
393 "chunk_size must be provided when using QuerySet.iterator() after "
394 "prefetch_related()."
395 )
396 with self.assertRaisesMessage(ValueError, msg):
397 Book.objects.prefetch_related("authors").iterator()
398
399 def test_m2m_join_reuse(self):
400 FavoriteAuthors.objects.bulk_create(
401 [
402 FavoriteAuthors(
403 author=self.author1, likes_author=self.author3, is_active=True
404 ),
405 FavoriteAuthors(
406 author=self.author1,
407 likes_author=self.author4,
408 is_active=False,
409 ),
410 FavoriteAuthors(
411 author=self.author2, likes_author=self.author3, is_active=True
412 ),
413 FavoriteAuthors(
414 author=self.author2, likes_author=self.author4, is_active=True
415 ),
416 ]
417 )
418 with self.assertNumQueries(2):
419 authors = list(
420 Author.objects.filter(
421 pk__in=[self.author1.pk, self.author2.pk]
422 ).prefetch_related(
423 Prefetch(
424 "favorite_authors",
425 queryset=(
426 Author.objects.annotate(
427 active_favorite=F("likes_me__is_active"),
428 ).filter(active_favorite=True)
429 ),
430 to_attr="active_favorite_authors",
431 )
432 )
433 )
434 self.assertEqual(authors[0].active_favorite_authors, [self.author3])
435 self.assertEqual(
436 authors[1].active_favorite_authors, [self.author3, self.author4]
437 )
438
439 def test_prefetch_queryset_child_class(self):
440 employee = SelfDirectedEmployee.objects.create(name="Foo")
441 employee.boss = employee
442 employee.save()
443 with self.assertNumQueries(2):
444 retrieved_employee = SelfDirectedEmployee.objects.prefetch_related(
445 Prefetch("boss", SelfDirectedEmployee.objects.all())
446 ).get()
447 with self.assertNumQueries(0):
448 self.assertEqual(retrieved_employee, employee)
449 self.assertEqual(retrieved_employee.boss, retrieved_employee)
450
451
452 class RawQuerySetTests(TestDataMixin, TestCase):
453 def test_basic(self):
454 with self.assertNumQueries(2):
455 books = Book.objects.raw(
456 "SELECT * FROM prefetch_related_book WHERE id = %s", (self.book1.id,)
457 ).prefetch_related("authors")
458 book1 = list(books)[0]
459
460 with self.assertNumQueries(0):
461 self.assertCountEqual(
462 book1.authors.all(), [self.author1, self.author2, self.author3]
463 )
464
465 def test_prefetch_before_raw(self):
466 with self.assertNumQueries(2):
467 books = Book.objects.prefetch_related("authors").raw(
468 "SELECT * FROM prefetch_related_book WHERE id = %s", (self.book1.id,)
469 )
470 book1 = list(books)[0]
471
472 with self.assertNumQueries(0):
473 self.assertCountEqual(
474 book1.authors.all(), [self.author1, self.author2, self.author3]
475 )
476
477 def test_clear(self):
478 with self.assertNumQueries(5):
479 with_prefetch = Author.objects.raw(
480 "SELECT * FROM prefetch_related_author"
481 ).prefetch_related("books")
482 without_prefetch = with_prefetch.prefetch_related(None)
483 [list(a.books.all()) for a in without_prefetch]
484
485
486 class CustomPrefetchTests(TestCase):
487 @classmethod
488 def traverse_qs(cls, obj_iter, path):
489 """
490 Helper method that returns a list containing a list of the objects in the
491 obj_iter. Then for each object in the obj_iter, the path will be
492 recursively travelled and the found objects are added to the return value.
493 """
494 ret_val = []
495
496 if hasattr(obj_iter, "all"):
497 obj_iter = obj_iter.all()
498
499 try:
500 iter(obj_iter)
501 except TypeError:
502 obj_iter = [obj_iter]
503
504 for obj in obj_iter:
505 rel_objs = []
506 for part in path:
507 if not part:
508 continue
509 try:
510 related = getattr(obj, part[0])
511 except ObjectDoesNotExist:
512 continue
513 if related is not None:
514 rel_objs.extend(cls.traverse_qs(related, [part[1:]]))
515 ret_val.append((obj, rel_objs))
516 return ret_val
517
518 @classmethod
519 def setUpTestData(cls):
520 cls.person1 = Person.objects.create(name="Joe")
521 cls.person2 = Person.objects.create(name="Mary")
522
523 # Set main_room for each house before creating the next one for
524 # databases where supports_nullable_unique_constraints is False.
525
526 cls.house1 = House.objects.create(
527 name="House 1", address="123 Main St", owner=cls.person1
528 )
529 cls.room1_1 = Room.objects.create(name="Dining room", house=cls.house1)
530 cls.room1_2 = Room.objects.create(name="Lounge", house=cls.house1)
531 cls.room1_3 = Room.objects.create(name="Kitchen", house=cls.house1)
532 cls.house1.main_room = cls.room1_1
533 cls.house1.save()
534 cls.person1.houses.add(cls.house1)
535
536 cls.house2 = House.objects.create(
537 name="House 2", address="45 Side St", owner=cls.person1
538 )
539 cls.room2_1 = Room.objects.create(name="Dining room", house=cls.house2)
540 cls.room2_2 = Room.objects.create(name="Lounge", house=cls.house2)
541 cls.room2_3 = Room.objects.create(name="Kitchen", house=cls.house2)
542 cls.house2.main_room = cls.room2_1
543 cls.house2.save()
544 cls.person1.houses.add(cls.house2)
545
546 cls.house3 = House.objects.create(
547 name="House 3", address="6 Downing St", owner=cls.person2
548 )
549 cls.room3_1 = Room.objects.create(name="Dining room", house=cls.house3)
550 cls.room3_2 = Room.objects.create(name="Lounge", house=cls.house3)
551 cls.room3_3 = Room.objects.create(name="Kitchen", house=cls.house3)
552 cls.house3.main_room = cls.room3_1
553 cls.house3.save()
554 cls.person2.houses.add(cls.house3)
555
556 cls.house4 = House.objects.create(
557 name="house 4", address="7 Regents St", owner=cls.person2
558 )
559 cls.room4_1 = Room.objects.create(name="Dining room", house=cls.house4)
560 cls.room4_2 = Room.objects.create(name="Lounge", house=cls.house4)
561 cls.room4_3 = Room.objects.create(name="Kitchen", house=cls.house4)
562 cls.house4.main_room = cls.room4_1
563 cls.house4.save()
564 cls.person2.houses.add(cls.house4)
565
566 def test_traverse_qs(self):
567 qs = Person.objects.prefetch_related("houses")
568 related_objs_normal = ([list(p.houses.all()) for p in qs],)
569 related_objs_from_traverse = [
570 [inner[0] for inner in o[1]] for o in self.traverse_qs(qs, [["houses"]])
571 ]
572 self.assertEqual(related_objs_normal, (related_objs_from_traverse,))
573
574 def test_ambiguous(self):
575 # Ambiguous: Lookup was already seen with a different queryset.
576 msg = (
577 "'houses' lookup was already seen with a different queryset. You "
578 "may need to adjust the ordering of your lookups."
579 )
580 # lookup.queryset shouldn't be evaluated.
581 with self.assertNumQueries(3):
582 with self.assertRaisesMessage(ValueError, msg):
583 self.traverse_qs(
584 Person.objects.prefetch_related(
585 "houses__rooms",
586 Prefetch("houses", queryset=House.objects.all()),
587 ),
588 [["houses", "rooms"]],
589 )
590
591 # Ambiguous: Lookup houses_lst doesn't yet exist when performing
592 # houses_lst__rooms.
593 msg = (
594 "Cannot find 'houses_lst' on Person object, 'houses_lst__rooms' is "
595 "an invalid parameter to prefetch_related()"
596 )
597 with self.assertRaisesMessage(AttributeError, msg):
598 self.traverse_qs(
599 Person.objects.prefetch_related(
600 "houses_lst__rooms",
601 Prefetch(
602 "houses", queryset=House.objects.all(), to_attr="houses_lst"
603 ),
604 ),
605 [["houses", "rooms"]],
606 )
607
608 # Not ambiguous.
609 self.traverse_qs(
610 Person.objects.prefetch_related("houses__rooms", "houses"),
611 [["houses", "rooms"]],
612 )
613
614 self.traverse_qs(
615 Person.objects.prefetch_related(
616 "houses__rooms",
617 Prefetch("houses", queryset=House.objects.all(), to_attr="houses_lst"),
618 ),
619 [["houses", "rooms"]],
620 )
621
622 def test_m2m(self):
623 # Control lookups.
624 with self.assertNumQueries(2):
625 lst1 = self.traverse_qs(
626 Person.objects.prefetch_related("houses"), [["houses"]]
627 )
628
629 # Test lookups.
630 with self.assertNumQueries(2):
631 lst2 = self.traverse_qs(
632 Person.objects.prefetch_related(Prefetch("houses")), [["houses"]]
633 )
634 self.assertEqual(lst1, lst2)
635 with self.assertNumQueries(2):
636 lst2 = self.traverse_qs(
637 Person.objects.prefetch_related(
638 Prefetch("houses", to_attr="houses_lst")
639 ),
640 [["houses_lst"]],
641 )
642 self.assertEqual(lst1, lst2)
643
644 def test_reverse_m2m(self):
645 # Control lookups.
646 with self.assertNumQueries(2):
647 lst1 = self.traverse_qs(
648 House.objects.prefetch_related("occupants"), [["occupants"]]
649 )
650
651 # Test lookups.
652 with self.assertNumQueries(2):
653 lst2 = self.traverse_qs(
654 House.objects.prefetch_related(Prefetch("occupants")), [["occupants"]]
655 )
656 self.assertEqual(lst1, lst2)
657 with self.assertNumQueries(2):
658 lst2 = self.traverse_qs(
659 House.objects.prefetch_related(
660 Prefetch("occupants", to_attr="occupants_lst")
661 ),
662 [["occupants_lst"]],
663 )
664 self.assertEqual(lst1, lst2)
665
666 def test_m2m_through_fk(self):
667 # Control lookups.
668 with self.assertNumQueries(3):
669 lst1 = self.traverse_qs(
670 Room.objects.prefetch_related("house__occupants"),
671 [["house", "occupants"]],
672 )
673
674 # Test lookups.
675 with self.assertNumQueries(3):
676 lst2 = self.traverse_qs(
677 Room.objects.prefetch_related(Prefetch("house__occupants")),
678 [["house", "occupants"]],
679 )
680 self.assertEqual(lst1, lst2)
681 with self.assertNumQueries(3):
682 lst2 = self.traverse_qs(
683 Room.objects.prefetch_related(
684 Prefetch("house__occupants", to_attr="occupants_lst")
685 ),
686 [["house", "occupants_lst"]],
687 )
688 self.assertEqual(lst1, lst2)
689
690 def test_m2m_through_gfk(self):
691 TaggedItem.objects.create(tag="houses", content_object=self.house1)
692 TaggedItem.objects.create(tag="houses", content_object=self.house2)
693
694 # Control lookups.
695 with self.assertNumQueries(3):
696 lst1 = self.traverse_qs(
697 TaggedItem.objects.filter(tag="houses").prefetch_related(
698 "content_object__rooms"
699 ),
700 [["content_object", "rooms"]],
701 )
702
703 # Test lookups.
704 with self.assertNumQueries(3):
705 lst2 = self.traverse_qs(
706 TaggedItem.objects.prefetch_related(
707 Prefetch("content_object"),
708 Prefetch("content_object__rooms", to_attr="rooms_lst"),
709 ),
710 [["content_object", "rooms_lst"]],
711 )
712 self.assertEqual(lst1, lst2)
713
714 def test_o2m_through_m2m(self):
715 # Control lookups.
716 with self.assertNumQueries(3):
717 lst1 = self.traverse_qs(
718 Person.objects.prefetch_related("houses", "houses__rooms"),
719 [["houses", "rooms"]],
720 )
721
722 # Test lookups.
723 with self.assertNumQueries(3):
724 lst2 = self.traverse_qs(
725 Person.objects.prefetch_related(Prefetch("houses"), "houses__rooms"),
726 [["houses", "rooms"]],
727 )
728 self.assertEqual(lst1, lst2)
729 with self.assertNumQueries(3):
730 lst2 = self.traverse_qs(
731 Person.objects.prefetch_related(
732 Prefetch("houses"), Prefetch("houses__rooms")
733 ),
734 [["houses", "rooms"]],
735 )
736 self.assertEqual(lst1, lst2)
737 with self.assertNumQueries(3):
738 lst2 = self.traverse_qs(
739 Person.objects.prefetch_related(
740 Prefetch("houses", to_attr="houses_lst"), "houses_lst__rooms"
741 ),
742 [["houses_lst", "rooms"]],
743 )
744 self.assertEqual(lst1, lst2)
745 with self.assertNumQueries(3):
746 lst2 = self.traverse_qs(
747 Person.objects.prefetch_related(
748 Prefetch("houses", to_attr="houses_lst"),
749 Prefetch("houses_lst__rooms", to_attr="rooms_lst"),
750 ),
751 [["houses_lst", "rooms_lst"]],
752 )
753 self.assertEqual(lst1, lst2)
754
755 def test_generic_rel(self):
756 bookmark = Bookmark.objects.create(url="http://www.djangoproject.com/")
757 TaggedItem.objects.create(content_object=bookmark, tag="django")
758 TaggedItem.objects.create(
759 content_object=bookmark, favorite=bookmark, tag="python"
760 )
761
762 # Control lookups.
763 with self.assertNumQueries(4):
764 lst1 = self.traverse_qs(
765 Bookmark.objects.prefetch_related(
766 "tags", "tags__content_object", "favorite_tags"
767 ),
768 [["tags", "content_object"], ["favorite_tags"]],
769 )
770
771 # Test lookups.
772 with self.assertNumQueries(4):
773 lst2 = self.traverse_qs(
774 Bookmark.objects.prefetch_related(
775 Prefetch("tags", to_attr="tags_lst"),
776 Prefetch("tags_lst__content_object"),
777 Prefetch("favorite_tags"),
778 ),
779 [["tags_lst", "content_object"], ["favorite_tags"]],
780 )
781 self.assertEqual(lst1, lst2)
782
783 def test_traverse_single_item_property(self):
784 # Control lookups.
785 with self.assertNumQueries(5):
786 lst1 = self.traverse_qs(
787 Person.objects.prefetch_related(
788 "houses__rooms",
789 "primary_house__occupants__houses",
790 ),
791 [["primary_house", "occupants", "houses"]],
792 )
793
794 # Test lookups.
795 with self.assertNumQueries(5):
796 lst2 = self.traverse_qs(
797 Person.objects.prefetch_related(
798 "houses__rooms",
799 Prefetch("primary_house__occupants", to_attr="occupants_lst"),
800 "primary_house__occupants_lst__houses",
801 ),
802 [["primary_house", "occupants_lst", "houses"]],
803 )
804 self.assertEqual(lst1, lst2)
805
806 def test_traverse_multiple_items_property(self):
807 # Control lookups.
808 with self.assertNumQueries(4):
809 lst1 = self.traverse_qs(
810 Person.objects.prefetch_related(
811 "houses",
812 "all_houses__occupants__houses",
813 ),
814 [["all_houses", "occupants", "houses"]],
815 )
816
817 # Test lookups.
818 with self.assertNumQueries(4):
819 lst2 = self.traverse_qs(
820 Person.objects.prefetch_related(
821 "houses",
822 Prefetch("all_houses__occupants", to_attr="occupants_lst"),
823 "all_houses__occupants_lst__houses",
824 ),
825 [["all_houses", "occupants_lst", "houses"]],
826 )
827 self.assertEqual(lst1, lst2)
828
829 def test_custom_qs(self):
830 # Test basic.
831 with self.assertNumQueries(2):
832 lst1 = list(Person.objects.prefetch_related("houses"))
833 with self.assertNumQueries(2):
834 lst2 = list(
835 Person.objects.prefetch_related(
836 Prefetch(
837 "houses", queryset=House.objects.all(), to_attr="houses_lst"
838 )
839 )
840 )
841 self.assertEqual(
842 self.traverse_qs(lst1, [["houses"]]),
843 self.traverse_qs(lst2, [["houses_lst"]]),
844 )
845
846 # Test queryset filtering.
847 with self.assertNumQueries(2):
848 lst2 = list(
849 Person.objects.prefetch_related(
850 Prefetch(
851 "houses",
852 queryset=House.objects.filter(
853 pk__in=[self.house1.pk, self.house3.pk]
854 ),
855 to_attr="houses_lst",
856 )
857 )
858 )
859 self.assertEqual(len(lst2[0].houses_lst), 1)
860 self.assertEqual(lst2[0].houses_lst[0], self.house1)
861 self.assertEqual(len(lst2[1].houses_lst), 1)
862 self.assertEqual(lst2[1].houses_lst[0], self.house3)
863
864 # Test flattened.
865 with self.assertNumQueries(3):
866 lst1 = list(Person.objects.prefetch_related("houses__rooms"))
867 with self.assertNumQueries(3):
868 lst2 = list(
869 Person.objects.prefetch_related(
870 Prefetch(
871 "houses__rooms",
872 queryset=Room.objects.all(),
873 to_attr="rooms_lst",
874 )
875 )
876 )
877 self.assertEqual(
878 self.traverse_qs(lst1, [["houses", "rooms"]]),
879 self.traverse_qs(lst2, [["houses", "rooms_lst"]]),
880 )
881
882 # Test inner select_related.
883 with self.assertNumQueries(3):
884 lst1 = list(Person.objects.prefetch_related("houses__owner"))
885 with self.assertNumQueries(2):
886 lst2 = list(
887 Person.objects.prefetch_related(
888 Prefetch("houses", queryset=House.objects.select_related("owner"))
889 )
890 )
891 self.assertEqual(
892 self.traverse_qs(lst1, [["houses", "owner"]]),
893 self.traverse_qs(lst2, [["houses", "owner"]]),
894 )
895
896 # Test inner prefetch.
897 inner_rooms_qs = Room.objects.filter(pk__in=[self.room1_1.pk, self.room1_2.pk])
898 houses_qs_prf = House.objects.prefetch_related(
899 Prefetch("rooms", queryset=inner_rooms_qs, to_attr="rooms_lst")
900 )
901 with self.assertNumQueries(4):
902 lst2 = list(
903 Person.objects.prefetch_related(
904 Prefetch(
905 "houses",
906 queryset=houses_qs_prf.filter(pk=self.house1.pk),
907 to_attr="houses_lst",
908 ),
909 Prefetch("houses_lst__rooms_lst__main_room_of"),
910 )
911 )
912
913 self.assertEqual(len(lst2[0].houses_lst[0].rooms_lst), 2)
914 self.assertEqual(lst2[0].houses_lst[0].rooms_lst[0], self.room1_1)
915 self.assertEqual(lst2[0].houses_lst[0].rooms_lst[1], self.room1_2)
916 self.assertEqual(lst2[0].houses_lst[0].rooms_lst[0].main_room_of, self.house1)
917 self.assertEqual(len(lst2[1].houses_lst), 0)
918
919 # Test ForwardManyToOneDescriptor.
920 houses = House.objects.select_related("owner")
921 with self.assertNumQueries(6):
922 rooms = Room.objects.prefetch_related("house")
923 lst1 = self.traverse_qs(rooms, [["house", "owner"]])
924 with self.assertNumQueries(2):
925 rooms = Room.objects.prefetch_related(Prefetch("house", queryset=houses))
926 lst2 = self.traverse_qs(rooms, [["house", "owner"]])
927 self.assertEqual(lst1, lst2)
928 with self.assertNumQueries(2):
929 houses = House.objects.select_related("owner")
930 rooms = Room.objects.prefetch_related(
931 Prefetch("house", queryset=houses, to_attr="house_attr")
932 )
933 lst2 = self.traverse_qs(rooms, [["house_attr", "owner"]])
934 self.assertEqual(lst1, lst2)
935 room = Room.objects.prefetch_related(
936 Prefetch("house", queryset=houses.filter(address="DoesNotExist"))
937 ).first()
938 with self.assertRaises(ObjectDoesNotExist):
939 getattr(room, "house")
940 room = Room.objects.prefetch_related(
941 Prefetch(
942 "house",
943 queryset=houses.filter(address="DoesNotExist"),
944 to_attr="house_attr",
945 )
946 ).first()
947 self.assertIsNone(room.house_attr)
948 rooms = Room.objects.prefetch_related(
949 Prefetch("house", queryset=House.objects.only("name"))
950 )
951 with self.assertNumQueries(2):
952 getattr(rooms.first().house, "name")
953 with self.assertNumQueries(3):
954 getattr(rooms.first().house, "address")
955
956 # Test ReverseOneToOneDescriptor.
957 houses = House.objects.select_related("owner")
958 with self.assertNumQueries(6):
959 rooms = Room.objects.prefetch_related("main_room_of")
960 lst1 = self.traverse_qs(rooms, [["main_room_of", "owner"]])
961 with self.assertNumQueries(2):
962 rooms = Room.objects.prefetch_related(
963 Prefetch("main_room_of", queryset=houses)
964 )
965 lst2 = self.traverse_qs(rooms, [["main_room_of", "owner"]])
966 self.assertEqual(lst1, lst2)
967 with self.assertNumQueries(2):
968 rooms = list(
969 Room.objects.prefetch_related(
970 Prefetch(
971 "main_room_of",
972 queryset=houses,
973 to_attr="main_room_of_attr",
974 )
975 )
976 )
977 lst2 = self.traverse_qs(rooms, [["main_room_of_attr", "owner"]])
978 self.assertEqual(lst1, lst2)
979 room = (
980 Room.objects.filter(main_room_of__isnull=False)
981 .prefetch_related(
982 Prefetch("main_room_of", queryset=houses.filter(address="DoesNotExist"))
983 )
984 .first()
985 )
986 with self.assertRaises(ObjectDoesNotExist):
987 getattr(room, "main_room_of")
988 room = (
989 Room.objects.filter(main_room_of__isnull=False)
990 .prefetch_related(
991 Prefetch(
992 "main_room_of",
993 queryset=houses.filter(address="DoesNotExist"),
994 to_attr="main_room_of_attr",
995 )
996 )
997 .first()
998 )
999 self.assertIsNone(room.main_room_of_attr)
1000
1001 # The custom queryset filters should be applied to the queryset
1002 # instance returned by the manager.
1003 person = Person.objects.prefetch_related(
1004 Prefetch("houses", queryset=House.objects.filter(name="House 1")),
1005 ).get(pk=self.person1.pk)
1006 self.assertEqual(
1007 list(person.houses.all()),
1008 list(person.houses.all().all()),
1009 )
1010
1011 def test_nested_prefetch_related_are_not_overwritten(self):
1012 # Regression test for #24873
1013 houses_2 = House.objects.prefetch_related(Prefetch("rooms"))
1014 persons = Person.objects.prefetch_related(Prefetch("houses", queryset=houses_2))
1015 houses = House.objects.prefetch_related(Prefetch("occupants", queryset=persons))
1016 list(houses) # queryset must be evaluated once to reproduce the bug.
1017 self.assertEqual(
1018 houses.all()[0].occupants.all()[0].houses.all()[1].rooms.all()[0],
1019 self.room2_1,
1020 )
1021
1022 def test_nested_prefetch_related_with_duplicate_prefetcher(self):
1023 """
1024 Nested prefetches whose name clashes with descriptor names
1025 (Person.houses here) are allowed.
1026 """
1027 occupants = Person.objects.prefetch_related(
1028 Prefetch("houses", to_attr="some_attr_name"),
1029 Prefetch("houses", queryset=House.objects.prefetch_related("main_room")),
1030 )
1031 houses = House.objects.prefetch_related(
1032 Prefetch("occupants", queryset=occupants)
1033 )
1034 with self.assertNumQueries(5):
1035 self.traverse_qs(list(houses), [["occupants", "houses", "main_room"]])
1036
1037 def test_nested_prefetch_related_with_duplicate_prefetch_and_depth(self):
1038 people = Person.objects.prefetch_related(
1039 Prefetch(
1040 "houses__main_room",
1041 queryset=Room.objects.filter(name="Dining room"),
1042 to_attr="dining_room",
1043 ),
1044 "houses__main_room",
1045 )
1046 with self.assertNumQueries(4):
1047 main_room = people[0].houses.all()[0]
1048
1049 people = Person.objects.prefetch_related(
1050 "houses__main_room",
1051 Prefetch(
1052 "houses__main_room",
1053 queryset=Room.objects.filter(name="Dining room"),
1054 to_attr="dining_room",
1055 ),
1056 )
1057 with self.assertNumQueries(4):
1058 main_room = people[0].houses.all()[0]
1059
1060 self.assertEqual(main_room.main_room, self.room1_1)
1061
1062 def test_values_queryset(self):
1063 msg = "Prefetch querysets cannot use raw(), values(), and values_list()."
1064 with self.assertRaisesMessage(ValueError, msg):
1065 Prefetch("houses", House.objects.values("pk"))
1066 with self.assertRaisesMessage(ValueError, msg):
1067 Prefetch("houses", House.objects.values_list("pk"))
1068 # That error doesn't affect managers with custom ModelIterable subclasses
1069 self.assertIs(
1070 Teacher.objects_custom.all()._iterable_class, ModelIterableSubclass
1071 )
1072 Prefetch("teachers", Teacher.objects_custom.all())
1073
1074 def test_raw_queryset(self):
1075 msg = "Prefetch querysets cannot use raw(), values(), and values_list()."
1076 with self.assertRaisesMessage(ValueError, msg):
1077 Prefetch("houses", House.objects.raw("select pk from house"))
1078
1079 def test_to_attr_doesnt_cache_through_attr_as_list(self):
1080 house = House.objects.prefetch_related(
1081 Prefetch("rooms", queryset=Room.objects.all(), to_attr="to_rooms"),
1082 ).get(pk=self.house3.pk)
1083 self.assertIsInstance(house.rooms.all(), QuerySet)
1084
1085 def test_to_attr_cached_property(self):
1086 persons = Person.objects.prefetch_related(
1087 Prefetch("houses", House.objects.all(), to_attr="cached_all_houses"),
1088 )
1089 for person in persons:
1090 # To bypass caching at the related descriptor level, don't use
1091 # person.houses.all() here.
1092 all_houses = list(House.objects.filter(occupants=person))
1093 with self.assertNumQueries(0):
1094 self.assertEqual(person.cached_all_houses, all_houses)
1095
1096 def test_filter_deferred(self):
1097 """
1098 Related filtering of prefetched querysets is deferred until necessary.
1099 """
1100 add_q = Query.add_q
1101 with mock.patch.object(
1102 Query,
1103 "add_q",
1104 autospec=True,
1105 side_effect=lambda self, q, reuse_all: add_q(self, q),
1106 ) as add_q_mock:
1107 list(
1108 House.objects.prefetch_related(
1109 Prefetch("occupants", queryset=Person.objects.all())
1110 )
1111 )
1112 self.assertEqual(add_q_mock.call_count, 1)
1113
1114
1115 class DefaultManagerTests(TestCase):
1116 @classmethod
1117 def setUpTestData(cls):
1118 cls.qual1 = Qualification.objects.create(name="BA")
1119 cls.qual2 = Qualification.objects.create(name="BSci")
1120 cls.qual3 = Qualification.objects.create(name="MA")
1121 cls.qual4 = Qualification.objects.create(name="PhD")
1122
1123 cls.teacher1 = Teacher.objects.create(name="Mr Cleese")
1124 cls.teacher2 = Teacher.objects.create(name="Mr Idle")
1125 cls.teacher3 = Teacher.objects.create(name="Mr Chapman")
1126 cls.teacher1.qualifications.add(cls.qual1, cls.qual2, cls.qual3, cls.qual4)
1127 cls.teacher2.qualifications.add(cls.qual1)
1128 cls.teacher3.qualifications.add(cls.qual2)
1129
1130 cls.dept1 = Department.objects.create(name="English")
1131 cls.dept2 = Department.objects.create(name="Physics")
1132 cls.dept1.teachers.add(cls.teacher1, cls.teacher2)
1133 cls.dept2.teachers.add(cls.teacher1, cls.teacher3)
1134
1135 def test_m2m_then_m2m(self):
1136 with self.assertNumQueries(3):
1137 # When we prefetch the teachers, and force the query, we don't want
1138 # the default manager on teachers to immediately get all the related
1139 # qualifications, since this will do one query per teacher.
1140 qs = Department.objects.prefetch_related("teachers")
1141 depts = "".join(
1142 "%s department: %s\n"
1143 % (dept.name, ", ".join(str(t) for t in dept.teachers.all()))
1144 for dept in qs
1145 )
1146
1147 self.assertEqual(
1148 depts,
1149 "English department: Mr Cleese (BA, BSci, MA, PhD), Mr Idle (BA)\n"
1150 "Physics department: Mr Cleese (BA, BSci, MA, PhD), Mr Chapman "
1151 "(BSci)\n",
1152 )
1153
1154
1155 class GenericRelationTests(TestCase):
1156 @classmethod
1157 def setUpTestData(cls):
1158 book1 = Book.objects.create(title="Winnie the Pooh")
1159 book2 = Book.objects.create(title="Do you like green eggs and spam?")
1160 book3 = Book.objects.create(title="Three Men In A Boat")
1161
1162 reader1 = Reader.objects.create(name="me")
1163 reader2 = Reader.objects.create(name="you")
1164 reader3 = Reader.objects.create(name="someone")
1165
1166 book1.read_by.add(reader1, reader2)
1167 book2.read_by.add(reader2)
1168 book3.read_by.add(reader3)
1169
1170 cls.book1, cls.book2, cls.book3 = book1, book2, book3
1171 cls.reader1, cls.reader2, cls.reader3 = reader1, reader2, reader3
1172
1173 def test_prefetch_GFK(self):
1174 TaggedItem.objects.create(tag="awesome", content_object=self.book1)
1175 TaggedItem.objects.create(tag="great", content_object=self.reader1)
1176 TaggedItem.objects.create(tag="outstanding", content_object=self.book2)
1177 TaggedItem.objects.create(tag="amazing", content_object=self.reader3)
1178
1179 # 1 for TaggedItem table, 1 for Book table, 1 for Reader table
1180 with self.assertNumQueries(3):
1181 qs = TaggedItem.objects.prefetch_related("content_object")
1182 list(qs)
1183
1184 def test_prefetch_GFK_nonint_pk(self):
1185 Comment.objects.create(comment="awesome", content_object=self.book1)
1186
1187 # 1 for Comment table, 1 for Book table
1188 with self.assertNumQueries(2):
1189 qs = Comment.objects.prefetch_related("content_object")
1190 [c.content_object for c in qs]
1191
1192 def test_prefetch_GFK_uuid_pk(self):
1193 article = Article.objects.create(name="Django")
1194 Comment.objects.create(comment="awesome", content_object_uuid=article)
1195 qs = Comment.objects.prefetch_related("content_object_uuid")
1196 self.assertEqual([c.content_object_uuid for c in qs], [article])
1197
1198 def test_prefetch_GFK_fk_pk(self):
1199 book = Book.objects.create(title="Poems")
1200 book_with_year = BookWithYear.objects.create(book=book, published_year=2019)
1201 Comment.objects.create(comment="awesome", content_object=book_with_year)
1202 qs = Comment.objects.prefetch_related("content_object")
1203 self.assertEqual([c.content_object for c in qs], [book_with_year])
1204
1205 def test_traverse_GFK(self):
1206 """
1207 A 'content_object' can be traversed with prefetch_related() and
1208 get to related objects on the other side (assuming it is suitably
1209 filtered)
1210 """
1211 TaggedItem.objects.create(tag="awesome", content_object=self.book1)
1212 TaggedItem.objects.create(tag="awesome", content_object=self.book2)
1213 TaggedItem.objects.create(tag="awesome", content_object=self.book3)
1214 TaggedItem.objects.create(tag="awesome", content_object=self.reader1)
1215 TaggedItem.objects.create(tag="awesome", content_object=self.reader2)
1216
1217 ct = ContentType.objects.get_for_model(Book)
1218
1219 # We get 3 queries - 1 for main query, 1 for content_objects since they
1220 # all use the same table, and 1 for the 'read_by' relation.
1221 with self.assertNumQueries(3):
1222 # If we limit to books, we know that they will have 'read_by'
1223 # attributes, so the following makes sense:
1224 qs = TaggedItem.objects.filter(
1225 content_type=ct, tag="awesome"
1226 ).prefetch_related("content_object__read_by")
1227 readers_of_awesome_books = {
1228 r.name for tag in qs for r in tag.content_object.read_by.all()
1229 }
1230 self.assertEqual(readers_of_awesome_books, {"me", "you", "someone"})
1231
1232 def test_nullable_GFK(self):
1233 TaggedItem.objects.create(
1234 tag="awesome", content_object=self.book1, created_by=self.reader1
1235 )
1236 TaggedItem.objects.create(tag="great", content_object=self.book2)
1237 TaggedItem.objects.create(tag="rubbish", content_object=self.book3)
1238
1239 with self.assertNumQueries(2):
1240 result = [
1241 t.created_by for t in TaggedItem.objects.prefetch_related("created_by")
1242 ]
1243
1244 self.assertEqual(result, [t.created_by for t in TaggedItem.objects.all()])
1245
1246 def test_generic_relation(self):
1247 bookmark = Bookmark.objects.create(url="http://www.djangoproject.com/")
1248 TaggedItem.objects.create(content_object=bookmark, tag="django")
1249 TaggedItem.objects.create(content_object=bookmark, tag="python")
1250
1251 with self.assertNumQueries(2):
1252 tags = [
1253 t.tag
1254 for b in Bookmark.objects.prefetch_related("tags")
1255 for t in b.tags.all()
1256 ]
1257 self.assertEqual(sorted(tags), ["django", "python"])
1258
1259 def test_charfield_GFK(self):
1260 b = Bookmark.objects.create(url="http://www.djangoproject.com/")
1261 TaggedItem.objects.create(content_object=b, tag="django")
1262 TaggedItem.objects.create(content_object=b, favorite=b, tag="python")
1263
1264 with self.assertNumQueries(3):
1265 bookmark = Bookmark.objects.filter(pk=b.pk).prefetch_related(
1266 "tags", "favorite_tags"
1267 )[0]
1268 self.assertEqual(
1269 sorted(i.tag for i in bookmark.tags.all()), ["django", "python"]
1270 )
1271 self.assertEqual([i.tag for i in bookmark.favorite_tags.all()], ["python"])
1272
1273 def test_custom_queryset(self):
1274 bookmark = Bookmark.objects.create(url="http://www.djangoproject.com/")
1275 django_tag = TaggedItem.objects.create(content_object=bookmark, tag="django")
1276 TaggedItem.objects.create(content_object=bookmark, tag="python")
1277
1278 with self.assertNumQueries(2):
1279 bookmark = Bookmark.objects.prefetch_related(
1280 Prefetch("tags", TaggedItem.objects.filter(tag="django")),
1281 ).get()
1282
1283 with self.assertNumQueries(0):
1284 self.assertEqual(list(bookmark.tags.all()), [django_tag])
1285
1286 # The custom queryset filters should be applied to the queryset
1287 # instance returned by the manager.
1288 self.assertEqual(list(bookmark.tags.all()), list(bookmark.tags.all().all()))
1289
1290 def test_deleted_GFK(self):
1291 TaggedItem.objects.create(tag="awesome", content_object=self.book1)
1292 TaggedItem.objects.create(tag="awesome", content_object=self.book2)
1293 ct = ContentType.objects.get_for_model(Book)
1294
1295 book1_pk = self.book1.pk
1296 self.book1.delete()
1297
1298 with self.assertNumQueries(2):
1299 qs = TaggedItem.objects.filter(tag="awesome").prefetch_related(
1300 "content_object"
1301 )
1302 result = [
1303 (tag.object_id, tag.content_type_id, tag.content_object) for tag in qs
1304 ]
1305 self.assertEqual(
1306 result,
1307 [
1308 (book1_pk, ct.pk, None),
1309 (self.book2.pk, ct.pk, self.book2),
1310 ],
1311 )
1312
1313 def test_reverse_generic_relation(self):
1314 # Create two distinct bookmarks to ensure the bookmark and
1315 # tagged item models primary are offset.
1316 first_bookmark = Bookmark.objects.create()
1317 second_bookmark = Bookmark.objects.create()
1318 TaggedItem.objects.create(
1319 content_object=first_bookmark, favorite=second_bookmark
1320 )
1321 with self.assertNumQueries(2):
1322 obj = TaggedItem.objects.prefetch_related("favorite_bookmarks").get()
1323 with self.assertNumQueries(0):
1324 prefetched_bookmarks = obj.favorite_bookmarks.all()
1325 self.assertQuerySetEqual(prefetched_bookmarks, [second_bookmark])
1326
1327
1328 class MultiTableInheritanceTest(TestCase):
1329 @classmethod
1330 def setUpTestData(cls):
1331 cls.book1 = BookWithYear.objects.create(title="Poems", published_year=2010)
1332 cls.book2 = BookWithYear.objects.create(title="More poems", published_year=2011)
1333 cls.author1 = AuthorWithAge.objects.create(
1334 name="Jane", first_book=cls.book1, age=50
1335 )
1336 cls.author2 = AuthorWithAge.objects.create(
1337 name="Tom", first_book=cls.book1, age=49
1338 )
1339 cls.author3 = AuthorWithAge.objects.create(
1340 name="Robert", first_book=cls.book2, age=48
1341 )
1342 cls.author_address = AuthorAddress.objects.create(
1343 author=cls.author1, address="SomeStreet 1"
1344 )
1345 cls.book2.aged_authors.add(cls.author2, cls.author3)
1346 cls.br1 = BookReview.objects.create(book=cls.book1, notes="review book1")
1347 cls.br2 = BookReview.objects.create(book=cls.book2, notes="review book2")
1348
1349 def test_foreignkey(self):
1350 with self.assertNumQueries(2):
1351 qs = AuthorWithAge.objects.prefetch_related("addresses")
1352 addresses = [
1353 [str(address) for address in obj.addresses.all()] for obj in qs
1354 ]
1355 self.assertEqual(addresses, [[str(self.author_address)], [], []])
1356
1357 def test_foreignkey_to_inherited(self):
1358 with self.assertNumQueries(2):
1359 qs = BookReview.objects.prefetch_related("book")
1360 titles = [obj.book.title for obj in qs]
1361 self.assertCountEqual(titles, ["Poems", "More poems"])
1362
1363 def test_m2m_to_inheriting_model(self):
1364 qs = AuthorWithAge.objects.prefetch_related("books_with_year")
1365 with self.assertNumQueries(2):
1366 lst = [
1367 [str(book) for book in author.books_with_year.all()] for author in qs
1368 ]
1369 qs = AuthorWithAge.objects.all()
1370 lst2 = [[str(book) for book in author.books_with_year.all()] for author in qs]
1371 self.assertEqual(lst, lst2)
1372
1373 qs = BookWithYear.objects.prefetch_related("aged_authors")
1374 with self.assertNumQueries(2):
1375 lst = [[str(author) for author in book.aged_authors.all()] for book in qs]
1376 qs = BookWithYear.objects.all()
1377 lst2 = [[str(author) for author in book.aged_authors.all()] for book in qs]
1378 self.assertEqual(lst, lst2)
1379
1380 def test_parent_link_prefetch(self):
1381 with self.assertNumQueries(2):
1382 [a.author for a in AuthorWithAge.objects.prefetch_related("author")]
1383
1384 @override_settings(DEBUG=True)
1385 def test_child_link_prefetch(self):
1386 with self.assertNumQueries(2):
1387 authors = [
1388 a.authorwithage
1389 for a in Author.objects.prefetch_related("authorwithage")
1390 ]
1391
1392 # Regression for #18090: the prefetching query must include an IN clause.
1393 # Note that on Oracle the table name is upper case in the generated SQL,
1394 # thus the .lower() call.
1395 self.assertIn("authorwithage", connection.queries[-1]["sql"].lower())
1396 self.assertIn(" IN ", connection.queries[-1]["sql"])
1397
1398 self.assertEqual(authors, [a.authorwithage for a in Author.objects.all()])
1399
1400
1401 class ForeignKeyToFieldTest(TestCase):
1402 @classmethod
1403 def setUpTestData(cls):
1404 cls.book = Book.objects.create(title="Poems")
1405 cls.author1 = Author.objects.create(name="Jane", first_book=cls.book)
1406 cls.author2 = Author.objects.create(name="Tom", first_book=cls.book)
1407 cls.author3 = Author.objects.create(name="Robert", first_book=cls.book)
1408 cls.author_address = AuthorAddress.objects.create(
1409 author=cls.author1, address="SomeStreet 1"
1410 )
1411 FavoriteAuthors.objects.create(author=cls.author1, likes_author=cls.author2)
1412 FavoriteAuthors.objects.create(author=cls.author2, likes_author=cls.author3)
1413 FavoriteAuthors.objects.create(author=cls.author3, likes_author=cls.author1)
1414
1415 def test_foreignkey(self):
1416 with self.assertNumQueries(2):
1417 qs = Author.objects.prefetch_related("addresses")
1418 addresses = [
1419 [str(address) for address in obj.addresses.all()] for obj in qs
1420 ]
1421 self.assertEqual(addresses, [[str(self.author_address)], [], []])
1422
1423 def test_m2m(self):
1424 with self.assertNumQueries(3):
1425 qs = Author.objects.prefetch_related("favorite_authors", "favors_me")
1426 favorites = [
1427 (
1428 [str(i_like) for i_like in author.favorite_authors.all()],
1429 [str(likes_me) for likes_me in author.favors_me.all()],
1430 )
1431 for author in qs
1432 ]
1433 self.assertEqual(
1434 favorites,
1435 [
1436 ([str(self.author2)], [str(self.author3)]),
1437 ([str(self.author3)], [str(self.author1)]),
1438 ([str(self.author1)], [str(self.author2)]),
1439 ],
1440 )
1441
1442
1443 class LookupOrderingTest(TestCase):
1444 """
1445 Test cases that demonstrate that ordering of lookups is important, and
1446 ensure it is preserved.
1447 """
1448
1449 @classmethod
1450 def setUpTestData(cls):
1451 person1 = Person.objects.create(name="Joe")
1452 person2 = Person.objects.create(name="Mary")
1453
1454 # Set main_room for each house before creating the next one for
1455 # databases where supports_nullable_unique_constraints is False.
1456 house1 = House.objects.create(address="123 Main St")
1457 room1_1 = Room.objects.create(name="Dining room", house=house1)
1458 Room.objects.create(name="Lounge", house=house1)
1459 Room.objects.create(name="Kitchen", house=house1)
1460 house1.main_room = room1_1
1461 house1.save()
1462 person1.houses.add(house1)
1463
1464 house2 = House.objects.create(address="45 Side St")
1465 room2_1 = Room.objects.create(name="Dining room", house=house2)
1466 Room.objects.create(name="Lounge", house=house2)
1467 house2.main_room = room2_1
1468 house2.save()
1469 person1.houses.add(house2)
1470
1471 house3 = House.objects.create(address="6 Downing St")
1472 room3_1 = Room.objects.create(name="Dining room", house=house3)
1473 Room.objects.create(name="Lounge", house=house3)
1474 Room.objects.create(name="Kitchen", house=house3)
1475 house3.main_room = room3_1
1476 house3.save()
1477 person2.houses.add(house3)
1478
1479 house4 = House.objects.create(address="7 Regents St")
1480 room4_1 = Room.objects.create(name="Dining room", house=house4)
1481 Room.objects.create(name="Lounge", house=house4)
1482 house4.main_room = room4_1
1483 house4.save()
1484 person2.houses.add(house4)
1485
1486 def test_order(self):
1487 with self.assertNumQueries(4):
1488 # The following two queries must be done in the same order as written,
1489 # otherwise 'primary_house' will cause non-prefetched lookups
1490 qs = Person.objects.prefetch_related(
1491 "houses__rooms", "primary_house__occupants"
1492 )
1493 [list(p.primary_house.occupants.all()) for p in qs]
1494
1495
1496 class NullableTest(TestCase):
1497 @classmethod
1498 def setUpTestData(cls):
1499 boss = Employee.objects.create(name="Peter")
1500 Employee.objects.create(name="Joe", boss=boss)
1501 Employee.objects.create(name="Angela", boss=boss)
1502
1503 def test_traverse_nullable(self):
1504 # Because we use select_related() for 'boss', it doesn't need to be
1505 # prefetched, but we can still traverse it although it contains some nulls
1506 with self.assertNumQueries(2):
1507 qs = Employee.objects.select_related("boss").prefetch_related("boss__serfs")
1508 co_serfs = [
1509 list(e.boss.serfs.all()) if e.boss is not None else [] for e in qs
1510 ]
1511
1512 qs2 = Employee.objects.select_related("boss")
1513 co_serfs2 = [
1514 list(e.boss.serfs.all()) if e.boss is not None else [] for e in qs2
1515 ]
1516
1517 self.assertEqual(co_serfs, co_serfs2)
1518
1519 def test_prefetch_nullable(self):
1520 # One for main employee, one for boss, one for serfs
1521 with self.assertNumQueries(3):
1522 qs = Employee.objects.prefetch_related("boss__serfs")
1523 co_serfs = [
1524 list(e.boss.serfs.all()) if e.boss is not None else [] for e in qs
1525 ]
1526
1527 qs2 = Employee.objects.all()
1528 co_serfs2 = [
1529 list(e.boss.serfs.all()) if e.boss is not None else [] for e in qs2
1530 ]
1531
1532 self.assertEqual(co_serfs, co_serfs2)
1533
1534 def test_in_bulk(self):
1535 """
1536 In-bulk does correctly prefetch objects by not using .iterator()
1537 directly.
1538 """
1539 boss1 = Employee.objects.create(name="Peter")
1540 boss2 = Employee.objects.create(name="Jack")
1541 with self.assertNumQueries(2):
1542 # Prefetch is done and it does not cause any errors.
1543 bulk = Employee.objects.prefetch_related("serfs").in_bulk(
1544 [boss1.pk, boss2.pk]
1545 )
1546 for b in bulk.values():
1547 list(b.serfs.all())
1548
1549
1550 class MultiDbTests(TestCase):
1551 databases = {"default", "other"}
1552
1553 def test_using_is_honored_m2m(self):
1554 B = Book.objects.using("other")
1555 A = Author.objects.using("other")
1556 book1 = B.create(title="Poems")
1557 book2 = B.create(title="Jane Eyre")
1558 book3 = B.create(title="Wuthering Heights")
1559 book4 = B.create(title="Sense and Sensibility")
1560
1561 author1 = A.create(name="Charlotte", first_book=book1)
1562 author2 = A.create(name="Anne", first_book=book1)
1563 author3 = A.create(name="Emily", first_book=book1)
1564 author4 = A.create(name="Jane", first_book=book4)
1565
1566 book1.authors.add(author1, author2, author3)
1567 book2.authors.add(author1)
1568 book3.authors.add(author3)
1569 book4.authors.add(author4)
1570
1571 # Forward
1572 qs1 = B.prefetch_related("authors")
1573 with self.assertNumQueries(2, using="other"):
1574 books = "".join(
1575 "%s (%s)\n"
1576 % (book.title, ", ".join(a.name for a in book.authors.all()))
1577 for book in qs1
1578 )
1579 self.assertEqual(
1580 books,
1581 "Poems (Charlotte, Anne, Emily)\n"
1582 "Jane Eyre (Charlotte)\n"
1583 "Wuthering Heights (Emily)\n"
1584 "Sense and Sensibility (Jane)\n",
1585 )
1586
1587 # Reverse
1588 qs2 = A.prefetch_related("books")
1589 with self.assertNumQueries(2, using="other"):
1590 authors = "".join(
1591 "%s: %s\n"
1592 % (author.name, ", ".join(b.title for b in author.books.all()))
1593 for author in qs2
1594 )
1595 self.assertEqual(
1596 authors,
1597 "Charlotte: Poems, Jane Eyre\n"
1598 "Anne: Poems\n"
1599 "Emily: Poems, Wuthering Heights\n"
1600 "Jane: Sense and Sensibility\n",
1601 )
1602
1603 def test_using_is_honored_fkey(self):
1604 B = Book.objects.using("other")
1605 A = Author.objects.using("other")
1606 book1 = B.create(title="Poems")
1607 book2 = B.create(title="Sense and Sensibility")
1608
1609 A.create(name="Charlotte Bronte", first_book=book1)
1610 A.create(name="Jane Austen", first_book=book2)
1611
1612 # Forward
1613 with self.assertNumQueries(2, using="other"):
1614 books = ", ".join(
1615 a.first_book.title for a in A.prefetch_related("first_book")
1616 )
1617 self.assertEqual("Poems, Sense and Sensibility", books)
1618
1619 # Reverse
1620 with self.assertNumQueries(2, using="other"):
1621 books = "".join(
1622 "%s (%s)\n"
1623 % (b.title, ", ".join(a.name for a in b.first_time_authors.all()))
1624 for b in B.prefetch_related("first_time_authors")
1625 )
1626 self.assertEqual(
1627 books,
1628 "Poems (Charlotte Bronte)\nSense and Sensibility (Jane Austen)\n",
1629 )
1630
1631 def test_using_is_honored_inheritance(self):
1632 B = BookWithYear.objects.using("other")
1633 A = AuthorWithAge.objects.using("other")
1634 book1 = B.create(title="Poems", published_year=2010)
1635 B.create(title="More poems", published_year=2011)
1636 A.create(name="Jane", first_book=book1, age=50)
1637 A.create(name="Tom", first_book=book1, age=49)
1638
1639 # parent link
1640 with self.assertNumQueries(2, using="other"):
1641 authors = ", ".join(a.author.name for a in A.prefetch_related("author"))
1642
1643 self.assertEqual(authors, "Jane, Tom")
1644
1645 # child link
1646 with self.assertNumQueries(2, using="other"):
1647 ages = ", ".join(
1648 str(a.authorwithage.age) for a in A.prefetch_related("authorwithage")
1649 )
1650
1651 self.assertEqual(ages, "50, 49")
1652
1653 def test_using_is_honored_custom_qs(self):
1654 B = Book.objects.using("other")
1655 A = Author.objects.using("other")
1656 book1 = B.create(title="Poems")
1657 book2 = B.create(title="Sense and Sensibility")
1658
1659 A.create(name="Charlotte Bronte", first_book=book1)
1660 A.create(name="Jane Austen", first_book=book2)
1661
1662 # Implicit hinting
1663 with self.assertNumQueries(2, using="other"):
1664 prefetch = Prefetch("first_time_authors", queryset=Author.objects.all())
1665 books = "".join(
1666 "%s (%s)\n"
1667 % (b.title, ", ".join(a.name for a in b.first_time_authors.all()))
1668 for b in B.prefetch_related(prefetch)
1669 )
1670 self.assertEqual(
1671 books,
1672 "Poems (Charlotte Bronte)\nSense and Sensibility (Jane Austen)\n",
1673 )
1674 # Explicit using on the same db.
1675 with self.assertNumQueries(2, using="other"):
1676 prefetch = Prefetch(
1677 "first_time_authors", queryset=Author.objects.using("other")
1678 )
1679 books = "".join(
1680 "%s (%s)\n"
1681 % (b.title, ", ".join(a.name for a in b.first_time_authors.all()))
1682 for b in B.prefetch_related(prefetch)
1683 )
1684 self.assertEqual(
1685 books,
1686 "Poems (Charlotte Bronte)\nSense and Sensibility (Jane Austen)\n",
1687 )
1688
1689 # Explicit using on a different db.
1690 with (
1691 self.assertNumQueries(1, using="default"),
1692 self.assertNumQueries(1, using="other"),
1693 ):
1694 prefetch = Prefetch(
1695 "first_time_authors", queryset=Author.objects.using("default")
1696 )
1697 books = "".join(
1698 "%s (%s)\n"
1699 % (b.title, ", ".join(a.name for a in b.first_time_authors.all()))
1700 for b in B.prefetch_related(prefetch)
1701 )
1702 self.assertEqual(books, "Poems ()\n" "Sense and Sensibility ()\n")
1703
1704
1705 class Ticket19607Tests(TestCase):
1706 @classmethod
1707 def setUpTestData(cls):
1708 LessonEntry.objects.bulk_create(
1709 LessonEntry(id=id_, name1=name1, name2=name2)
1710 for id_, name1, name2 in [
1711 (1, "einfach", "simple"),
1712 (2, "schwierig", "difficult"),
1713 ]
1714 )
1715 WordEntry.objects.bulk_create(
1716 WordEntry(id=id_, lesson_entry_id=lesson_entry_id, name=name)
1717 for id_, lesson_entry_id, name in [
1718 (1, 1, "einfach"),
1719 (2, 1, "simple"),
1720 (3, 2, "schwierig"),
1721 (4, 2, "difficult"),
1722 ]
1723 )
1724
1725 def test_bug(self):
1726 list(
1727 WordEntry.objects.prefetch_related(
1728 "lesson_entry", "lesson_entry__wordentry_set"
1729 )
1730 )
1731
1732
1733 class Ticket21410Tests(TestCase):
1734 @classmethod
1735 def setUpTestData(cls):
1736 book1 = Book.objects.create(title="Poems")
1737 book2 = Book.objects.create(title="Jane Eyre")
1738 book3 = Book.objects.create(title="Wuthering Heights")
1739 book4 = Book.objects.create(title="Sense and Sensibility")
1740
1741 author1 = Author2.objects.create(name="Charlotte", first_book=book1)
1742 author2 = Author2.objects.create(name="Anne", first_book=book1)
1743 author3 = Author2.objects.create(name="Emily", first_book=book1)
1744 author4 = Author2.objects.create(name="Jane", first_book=book4)
1745
1746 author1.favorite_books.add(book1, book2, book3)
1747 author2.favorite_books.add(book1)
1748 author3.favorite_books.add(book2)
1749 author4.favorite_books.add(book3)
1750
1751 def test_bug(self):
1752 list(Author2.objects.prefetch_related("first_book", "favorite_books"))
1753
1754
1755 class Ticket21760Tests(TestCase):
1756 @classmethod
1757 def setUpTestData(cls):
1758 cls.rooms = []
1759 for _ in range(3):
1760 house = House.objects.create()
1761 for _ in range(3):
1762 cls.rooms.append(Room.objects.create(house=house))
1763 # Set main_room for each house before creating the next one for
1764 # databases where supports_nullable_unique_constraints is False.
1765 house.main_room = cls.rooms[-3]
1766 house.save()
1767
1768 def test_bug(self):
1769 prefetcher = get_prefetcher(self.rooms[0], "house", "house")[0]
1770 queryset = prefetcher.get_prefetch_querysets(list(Room.objects.all()))[0]
1771 self.assertNotIn(" JOIN ", str(queryset.query))
1772
1773
1774 class DirectPrefetchedObjectCacheReuseTests(TestCase):
1775 """
1776 prefetch_related() reuses objects fetched in _prefetched_objects_cache.
1777
1778 When objects are prefetched and not stored as an instance attribute (often
1779 intermediary relationships), they are saved to the
1780 _prefetched_objects_cache attribute. prefetch_related() takes
1781 _prefetched_objects_cache into account when determining whether an object
1782 has been fetched[1] and retrieves results from it when it is populated [2].
1783
1784 [1]: #25546 (duplicate queries on nested Prefetch)
1785 [2]: #27554 (queryset evaluation fails with a mix of nested and flattened
1786 prefetches)
1787 """
1788
1789 @classmethod
1790 def setUpTestData(cls):
1791 cls.book1, cls.book2 = [
1792 Book.objects.create(title="book1"),
1793 Book.objects.create(title="book2"),
1794 ]
1795 cls.author11, cls.author12, cls.author21 = [
1796 Author.objects.create(first_book=cls.book1, name="Author11"),
1797 Author.objects.create(first_book=cls.book1, name="Author12"),
1798 Author.objects.create(first_book=cls.book2, name="Author21"),
1799 ]
1800 cls.author1_address1, cls.author1_address2, cls.author2_address1 = [
1801 AuthorAddress.objects.create(author=cls.author11, address="Happy place"),
1802 AuthorAddress.objects.create(author=cls.author12, address="Haunted house"),
1803 AuthorAddress.objects.create(author=cls.author21, address="Happy place"),
1804 ]
1805 cls.bookwithyear1 = BookWithYear.objects.create(
1806 title="Poems", published_year=2010
1807 )
1808 cls.bookreview1 = BookReview.objects.create(book=cls.bookwithyear1)
1809
1810 def test_detect_is_fetched(self):
1811 """
1812 Nested prefetch_related() shouldn't trigger duplicate queries for the same
1813 lookup.
1814 """
1815 with self.assertNumQueries(3):
1816 books = Book.objects.filter(title__in=["book1", "book2"]).prefetch_related(
1817 Prefetch(
1818 "first_time_authors",
1819 Author.objects.prefetch_related(
1820 Prefetch(
1821 "addresses",
1822 AuthorAddress.objects.filter(address="Happy place"),
1823 )
1824 ),
1825 ),
1826 )
1827 book1, book2 = list(books)
1828
1829 with self.assertNumQueries(0):
1830 self.assertSequenceEqual(
1831 book1.first_time_authors.all(), [self.author11, self.author12]
1832 )
1833 self.assertSequenceEqual(book2.first_time_authors.all(), [self.author21])
1834
1835 self.assertSequenceEqual(
1836 book1.first_time_authors.all()[0].addresses.all(),
1837 [self.author1_address1],
1838 )
1839 self.assertSequenceEqual(
1840 book1.first_time_authors.all()[1].addresses.all(), []
1841 )
1842 self.assertSequenceEqual(
1843 book2.first_time_authors.all()[0].addresses.all(),
1844 [self.author2_address1],
1845 )
1846
1847 self.assertEqual(
1848 list(book1.first_time_authors.all()),
1849 list(book1.first_time_authors.all().all()),
1850 )
1851 self.assertEqual(
1852 list(book2.first_time_authors.all()),
1853 list(book2.first_time_authors.all().all()),
1854 )
1855 self.assertEqual(
1856 list(book1.first_time_authors.all()[0].addresses.all()),
1857 list(book1.first_time_authors.all()[0].addresses.all().all()),
1858 )
1859 self.assertEqual(
1860 list(book1.first_time_authors.all()[1].addresses.all()),
1861 list(book1.first_time_authors.all()[1].addresses.all().all()),
1862 )
1863 self.assertEqual(
1864 list(book2.first_time_authors.all()[0].addresses.all()),
1865 list(book2.first_time_authors.all()[0].addresses.all().all()),
1866 )
1867
1868 def test_detect_is_fetched_with_to_attr(self):
1869 with self.assertNumQueries(3):
1870 books = Book.objects.filter(title__in=["book1", "book2"]).prefetch_related(
1871 Prefetch(
1872 "first_time_authors",
1873 Author.objects.prefetch_related(
1874 Prefetch(
1875 "addresses",
1876 AuthorAddress.objects.filter(address="Happy place"),
1877 to_attr="happy_place",
1878 )
1879 ),
1880 to_attr="first_authors",
1881 ),
1882 )
1883 book1, book2 = list(books)
1884
1885 with self.assertNumQueries(0):
1886 self.assertEqual(book1.first_authors, [self.author11, self.author12])
1887 self.assertEqual(book2.first_authors, [self.author21])
1888
1889 self.assertEqual(
1890 book1.first_authors[0].happy_place, [self.author1_address1]
1891 )
1892 self.assertEqual(book1.first_authors[1].happy_place, [])
1893 self.assertEqual(
1894 book2.first_authors[0].happy_place, [self.author2_address1]
1895 )
1896
1897 def test_prefetch_reverse_foreign_key(self):
1898 with self.assertNumQueries(2):
1899 (bookwithyear1,) = BookWithYear.objects.prefetch_related("bookreview_set")
1900 with self.assertNumQueries(0):
1901 self.assertCountEqual(
1902 bookwithyear1.bookreview_set.all(), [self.bookreview1]
1903 )
1904 with self.assertNumQueries(0):
1905 prefetch_related_objects([bookwithyear1], "bookreview_set")
1906
1907 def test_add_clears_prefetched_objects(self):
1908 bookwithyear = BookWithYear.objects.get(pk=self.bookwithyear1.pk)
1909 prefetch_related_objects([bookwithyear], "bookreview_set")
1910 self.assertCountEqual(bookwithyear.bookreview_set.all(), [self.bookreview1])
1911 new_review = BookReview.objects.create()
1912 bookwithyear.bookreview_set.add(new_review)
1913 self.assertCountEqual(
1914 bookwithyear.bookreview_set.all(), [self.bookreview1, new_review]
1915 )
1916
1917 def test_remove_clears_prefetched_objects(self):
1918 bookwithyear = BookWithYear.objects.get(pk=self.bookwithyear1.pk)
1919 prefetch_related_objects([bookwithyear], "bookreview_set")
1920 self.assertCountEqual(bookwithyear.bookreview_set.all(), [self.bookreview1])
1921 bookwithyear.bookreview_set.remove(self.bookreview1)
1922 self.assertCountEqual(bookwithyear.bookreview_set.all(), [])
1923
1924
1925 class ReadPrefetchedObjectsCacheTests(TestCase):
1926 @classmethod
1927 def setUpTestData(cls):
1928 cls.book1 = Book.objects.create(title="Les confessions Volume I")
1929 cls.book2 = Book.objects.create(title="Candide")
1930 cls.author1 = AuthorWithAge.objects.create(
1931 name="Rousseau", first_book=cls.book1, age=70
1932 )
1933 cls.author2 = AuthorWithAge.objects.create(
1934 name="Voltaire", first_book=cls.book2, age=65
1935 )
1936 cls.book1.authors.add(cls.author1)
1937 cls.book2.authors.add(cls.author2)
1938 FavoriteAuthors.objects.create(author=cls.author1, likes_author=cls.author2)
1939
1940 def test_retrieves_results_from_prefetched_objects_cache(self):
1941 """
1942 When intermediary results are prefetched without a destination
1943 attribute, they are saved in the RelatedManager's cache
1944 (_prefetched_objects_cache). prefetch_related() uses this cache
1945 (#27554).
1946 """
1947 authors = AuthorWithAge.objects.prefetch_related(
1948 Prefetch(
1949 "author",
1950 queryset=Author.objects.prefetch_related(
1951 # Results are saved in the RelatedManager's cache
1952 # (_prefetched_objects_cache) and do not replace the
1953 # RelatedManager on Author instances (favorite_authors)
1954 Prefetch("favorite_authors__first_book"),
1955 ),
1956 ),
1957 )
1958 with self.assertNumQueries(4):
1959 # AuthorWithAge -> Author -> FavoriteAuthors, Book
1960 self.assertSequenceEqual(authors, [self.author1, self.author2])
1961
1962
1963 class NestedPrefetchTests(TestCase):
1964 @classmethod
1965 def setUpTestData(cls):
1966 house = House.objects.create(name="Big house", address="123 Main St")
1967 cls.room = Room.objects.create(name="Kitchen", house=house)
1968
1969 def test_nested_prefetch_is_not_overwritten_by_related_object(self):
1970 """
1971 The prefetched relationship is used rather than populating the reverse
1972 relationship from the parent, when prefetching a set of child objects
1973 related to a set of parent objects and the child queryset itself
1974 specifies a prefetch back to the parent.
1975 """
1976 queryset = House.objects.only("name").prefetch_related(
1977 Prefetch(
1978 "rooms",
1979 queryset=Room.objects.prefetch_related(
1980 Prefetch("house", queryset=House.objects.only("address")),
1981 ),
1982 ),
1983 )
1984 with self.assertNumQueries(3):
1985 house = queryset.first()
1986
1987 self.assertIs(Room.house.is_cached(self.room), True)
1988 with self.assertNumQueries(0):
1989 house.rooms.first().house.address
1990
1991
1992 class PrefetchLimitTests(TestDataMixin, TestCase):
1993 @skipUnlessDBFeature("supports_over_clause")
1994 def test_m2m_forward(self):
1995 authors = Author.objects.all() # Meta.ordering
1996 with self.assertNumQueries(3):
1997 books = list(
1998 Book.objects.prefetch_related(
1999 Prefetch("authors", authors),
2000 Prefetch("authors", authors[1:], to_attr="authors_sliced"),
2001 )
2002 )
2003 for book in books:
2004 with self.subTest(book=book):
2005 self.assertEqual(book.authors_sliced, list(book.authors.all())[1:])
2006
2007 @skipUnlessDBFeature("supports_over_clause")
2008 def test_m2m_reverse(self):
2009 books = Book.objects.order_by("title")
2010 with self.assertNumQueries(3):
2011 authors = list(
2012 Author.objects.prefetch_related(
2013 Prefetch("books", books),
2014 Prefetch("books", books[1:2], to_attr="books_sliced"),
2015 )
2016 )
2017 for author in authors:
2018 with self.subTest(author=author):
2019 self.assertEqual(author.books_sliced, list(author.books.all())[1:2])
2020
2021 @skipUnlessDBFeature("supports_over_clause")
2022 def test_foreignkey_reverse(self):
2023 authors = Author.objects.order_by("-name")
2024 with self.assertNumQueries(3):
2025 books = list(
2026 Book.objects.prefetch_related(
2027 Prefetch(
2028 "first_time_authors",
2029 authors,
2030 ),
2031 Prefetch(
2032 "first_time_authors",
2033 authors[1:],
2034 to_attr="first_time_authors_sliced",
2035 ),
2036 )
2037 )
2038 for book in books:
2039 with self.subTest(book=book):
2040 self.assertEqual(
2041 book.first_time_authors_sliced,
2042 list(book.first_time_authors.all())[1:],
2043 )
2044
2045 @skipUnlessDBFeature("supports_over_clause")
2046 def test_reverse_ordering(self):
2047 authors = Author.objects.reverse() # Reverse Meta.ordering
2048 with self.assertNumQueries(3):
2049 books = list(
2050 Book.objects.prefetch_related(
2051 Prefetch("authors", authors),
2052 Prefetch("authors", authors[1:], to_attr="authors_sliced"),
2053 )
2054 )
2055 for book in books:
2056 with self.subTest(book=book):
2057 self.assertEqual(book.authors_sliced, list(book.authors.all())[1:])
2058
2059 @skipIfDBFeature("supports_over_clause")
2060 def test_window_not_supported(self):
2061 authors = Author.objects.all()
2062 msg = (
2063 "Prefetching from a limited queryset is only supported on backends that "
2064 "support window functions."
2065 )
2066 with self.assertRaisesMessage(NotSupportedError, msg):
2067 list(Book.objects.prefetch_related(Prefetch("authors", authors[1:])))
2068
2069 @skipUnlessDBFeature("supports_over_clause")
2070 def test_empty_order(self):
2071 authors = Author.objects.order_by()
2072 with self.assertNumQueries(3):
2073 books = list(
2074 Book.objects.prefetch_related(
2075 Prefetch("authors", authors),
2076 Prefetch("authors", authors[:1], to_attr="authors_sliced"),
2077 )
2078 )
2079 for book in books:
2080 with self.subTest(book=book):
2081 self.assertEqual(len(book.authors_sliced), 1)
2082 self.assertIn(book.authors_sliced[0], list(book.authors.all()))
2083
2084
2085 class DeprecationTests(TestCase):
2086 def test_get_current_queryset_warning(self):
2087 msg = (
2088 "Prefetch.get_current_queryset() is deprecated. Use "
2089 "get_current_querysets() instead."
2090 )
2091 authors = Author.objects.all()
2092 with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
2093 self.assertEqual(
2094 Prefetch("authors", authors).get_current_queryset(1),
2095 authors,
2096 )
2097 self.assertEqual(ctx.filename, __file__)
2098 with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
2099 self.assertIsNone(Prefetch("authors").get_current_queryset(1))
2100 self.assertEqual(ctx.filename, __file__)
2101
2102 @ignore_warnings(category=RemovedInDjango60Warning)
2103 def test_prefetch_one_level_fallback(self):
2104 class NoGetPrefetchQuerySetsDescriptor(ForwardManyToOneDescriptor):
2105 def get_prefetch_queryset(self, instances, queryset=None):
2106 if queryset is None:
2107 return super().get_prefetch_querysets(instances)
2108 return super().get_prefetch_querysets(instances, [queryset])
2109
2110 def __getattribute__(self, name):
2111 if name == "get_prefetch_querysets":
2112 raise AttributeError
2113 return super().__getattribute__(name)
2114
2115 house = House.objects.create()
2116 room = Room.objects.create(house=house)
2117 house.main_room = room
2118 house.save()
2119
2120 # prefetch_one_level() fallbacks to get_prefetch_queryset().
2121 prefetcher = NoGetPrefetchQuerySetsDescriptor(Room._meta.get_field("house"))
2122 obj_list, additional_lookups = prefetch_one_level(
2123 [room],
2124 prefetcher,
2125 Prefetch("house", House.objects.all()),
2126 0,
2127 )
2128 self.assertEqual(obj_list, [house])
2129 self.assertEqual(additional_lookups, [])
2130
2131 obj_list, additional_lookups = prefetch_one_level(
2132 [room],
2133 prefetcher,
2134 Prefetch("house"),
2135 0,
2136 )
2137 self.assertEqual(obj_list, [house])
2138 self.assertEqual(additional_lookups, [])