summaryrefslogtreecommitdiff
path: root/tests/prefetch_related
diff options
context:
space:
mode:
authorMaxime Toussaint <maxime.toussaint@tlmgo.com>2023-08-31 08:11:03 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-09-07 12:56:08 +0200
commit254df3a3bbc2f4df51f9e2070ab2e214553f67d0 (patch)
tree72e696ac792fcd393e387aa151ab81496964a846 /tests/prefetch_related
parentf333e3513e8bdf5ffeb6eeb63021c230082e6f95 (diff)
Fixed #34791 -- Fixed incorrect Prefetch()'s cache for singly related objects.
Changed the cache name used for singly related objects to be the to_attr parameter passed to a Prefetch object. This fixes issues with checking if values have already been fetched in cases where the Field already has some prefetched value, but not for the same model attr.
Diffstat (limited to 'tests/prefetch_related')
-rw-r--r--tests/prefetch_related/tests.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py
index c8b9a9538b..4566de631e 100644
--- a/tests/prefetch_related/tests.py
+++ b/tests/prefetch_related/tests.py
@@ -978,6 +978,31 @@ class CustomPrefetchTests(TestCase):
with self.assertNumQueries(5):
self.traverse_qs(list(houses), [["occupants", "houses", "main_room"]])
+ def test_nested_prefetch_related_with_duplicate_prefetch_and_depth(self):
+ people = Person.objects.prefetch_related(
+ Prefetch(
+ "houses__main_room",
+ queryset=Room.objects.filter(name="Dining room"),
+ to_attr="dining_room",
+ ),
+ "houses__main_room",
+ )
+ with self.assertNumQueries(4):
+ main_room = people[0].houses.all()[0]
+
+ people = Person.objects.prefetch_related(
+ "houses__main_room",
+ Prefetch(
+ "houses__main_room",
+ queryset=Room.objects.filter(name="Dining room"),
+ to_attr="dining_room",
+ ),
+ )
+ with self.assertNumQueries(4):
+ main_room = people[0].houses.all()[0]
+
+ self.assertEqual(main_room.main_room, self.room1_1)
+
def test_values_queryset(self):
msg = "Prefetch querysets cannot use raw(), values(), and values_list()."
with self.assertRaisesMessage(ValueError, msg):