diff --git a/django/forms/models.py b/django/forms/models.py index c546b3972a..8134cf3de3 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -1130,7 +1130,7 @@ class ModelChoiceIterator: def __iter__(self): if self.field.empty_label is not None: yield ("", self.field.empty_label) - queryset = self.queryset.all() + queryset = self.queryset # Can't use iterator() when queryset uses prefetch_related() if not queryset._prefetch_related_lookups: queryset = queryset.iterator() @@ -1194,7 +1194,7 @@ class ModelChoiceField(ChoiceField): return self._queryset def _set_queryset(self, queryset): - self._queryset = queryset + self._queryset = None if queryset is None else queryset.all() self.widget.choices = self.choices queryset = property(_get_queryset, _set_queryset) diff --git a/tests/model_forms/test_modelchoicefield.py b/tests/model_forms/test_modelchoicefield.py index 057eb0d25b..6b0f00cf7f 100644 --- a/tests/model_forms/test_modelchoicefield.py +++ b/tests/model_forms/test_modelchoicefield.py @@ -249,6 +249,7 @@ class ModelChoiceFieldTests(TestCase): def test_queryset_manager(self): f = forms.ModelChoiceField(Category.objects) + self.assertEqual(len(f.choices), 4) self.assertEqual(list(f.choices), [ ('', '---------'), (self.c1.pk, 'Entertainment'), diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index fced7403ba..e3c347aee6 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -2339,7 +2339,7 @@ class OtherModelFormTests(TestCase): return ', '.join(c.name for c in obj.colours.all()) field = ColorModelChoiceField(ColourfulItem.objects.prefetch_related('colours')) - with self.assertNumQueries(4): # would be 5 if prefetch is ignored + with self.assertNumQueries(2): # would be 3 if prefetch is ignored self.assertEqual(tuple(field.choices), ( ('', '---------'), (multicolor_item.pk, 'blue, red'),