1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

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.
This commit is contained in:
Maxime Toussaint
2023-08-31 08:11:03 -04:00
committed by Mariusz Felisiak
parent f333e3513e
commit 254df3a3bb
3 changed files with 48 additions and 16 deletions

View File

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