mirror of https://github.com/django/django.git
Fixed #30482 -- Prevented unnecessary evaluation of lookup.queryset in prefetch_related_objects().
This commit is contained in:
parent
4442ee8a51
commit
59ffafa1d2
|
@ -1556,7 +1556,7 @@ def prefetch_related_objects(model_instances, *related_lookups):
|
||||||
while all_lookups:
|
while all_lookups:
|
||||||
lookup = all_lookups.pop()
|
lookup = all_lookups.pop()
|
||||||
if lookup.prefetch_to in done_queries:
|
if lookup.prefetch_to in done_queries:
|
||||||
if lookup.queryset:
|
if lookup.queryset is not None:
|
||||||
raise ValueError("'%s' lookup was already seen with a different queryset. "
|
raise ValueError("'%s' lookup was already seen with a different queryset. "
|
||||||
"You may need to adjust the ordering of your lookups." % lookup.prefetch_to)
|
"You may need to adjust the ordering of your lookups." % lookup.prefetch_to)
|
||||||
|
|
||||||
|
|
|
@ -400,11 +400,16 @@ class CustomPrefetchTests(TestCase):
|
||||||
"'houses' lookup was already seen with a different queryset. You "
|
"'houses' lookup was already seen with a different queryset. You "
|
||||||
"may need to adjust the ordering of your lookups."
|
"may need to adjust the ordering of your lookups."
|
||||||
)
|
)
|
||||||
with self.assertRaisesMessage(ValueError, msg):
|
# lookup.queryset shouldn't be evaluated.
|
||||||
self.traverse_qs(
|
with self.assertNumQueries(3):
|
||||||
Person.objects.prefetch_related('houses__rooms', Prefetch('houses', queryset=House.objects.all())),
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
[['houses', 'rooms']]
|
self.traverse_qs(
|
||||||
)
|
Person.objects.prefetch_related(
|
||||||
|
'houses__rooms',
|
||||||
|
Prefetch('houses', queryset=House.objects.all()),
|
||||||
|
),
|
||||||
|
[['houses', 'rooms']],
|
||||||
|
)
|
||||||
|
|
||||||
# Ambiguous: Lookup houses_lst doesn't yet exist when performing houses_lst__rooms.
|
# Ambiguous: Lookup houses_lst doesn't yet exist when performing houses_lst__rooms.
|
||||||
msg = (
|
msg = (
|
||||||
|
|
Loading…
Reference in New Issue