mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
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:
committed by
Tim Graham
parent
40f0aa9885
commit
a2e97abd81
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user