Fixed #29159 -- Made ModelChoiceIterator reuse QuerySet result cache.
When __len__() is called (e.g. when casting to list or tuple), the QuerySet is evaluated and the result cache populated. iterator() shouldn't be called on the QuerySet after that, as it would reset the result cache and trigger a second query.
This commit is contained in:
parent
40f0aa9885
commit
a2e97abd81
|
@ -1132,7 +1132,7 @@ class ModelChoiceIterator:
|
|||
yield ("", self.field.empty_label)
|
||||
queryset = self.queryset
|
||||
# Can't use iterator() when queryset uses prefetch_related()
|
||||
if not queryset._prefetch_related_lookups:
|
||||
if not queryset._prefetch_related_lookups and queryset._result_cache is None:
|
||||
queryset = queryset.iterator()
|
||||
for obj in queryset:
|
||||
yield self.choice(obj)
|
||||
|
|
|
@ -257,6 +257,17 @@ class ModelChoiceFieldTests(TestCase):
|
|||
(self.c3.pk, 'Third'),
|
||||
])
|
||||
|
||||
def test_queryset_result_cache_is_reused(self):
|
||||
f = forms.ModelChoiceField(Category.objects.all())
|
||||
with self.assertNumQueries(1):
|
||||
# list() calls __len__() and __iter__(); no duplicate queries.
|
||||
self.assertEqual(list(f.choices), [
|
||||
('', '---------'),
|
||||
(self.c1.pk, 'Entertainment'),
|
||||
(self.c2.pk, 'A test'),
|
||||
(self.c3.pk, 'Third'),
|
||||
])
|
||||
|
||||
def test_num_queries(self):
|
||||
"""
|
||||
Widgets that render multiple subwidgets shouldn't make more than one
|
||||
|
|
Loading…
Reference in New Issue