diff --git a/django/forms/models.py b/django/forms/models.py index 1036e9b2886..384dddf2426 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -1092,17 +1092,19 @@ class ModelChoiceIterator(object): def __iter__(self): if self.field.empty_label is not None: yield ("", self.field.empty_label) - method = 'all' if self.queryset._prefetch_related_lookups else 'iterator' - queryset = getattr(self.queryset, method) + queryset = self.queryset.all() + # Can't use iterator() when queryset uses prefetch_related() + if not queryset._prefetch_related_lookups: + queryset = queryset.iterator() if self.field.cache_choices: if self.field.choice_cache is None: self.field.choice_cache = [ - self.choice(obj) for obj in queryset() + self.choice(obj) for obj in queryset ] for choice in self.field.choice_cache: yield choice else: - for obj in queryset(): + for obj in queryset: yield self.choice(obj) def __len__(self): diff --git a/docs/releases/1.8.7.txt b/docs/releases/1.8.7.txt index f3cfc8e046e..9e8f37f1203 100644 --- a/docs/releases/1.8.7.txt +++ b/docs/releases/1.8.7.txt @@ -14,3 +14,7 @@ Bugfixes * Fixed a regression in 1.8.6 that caused database routers without an ``allow_migrate()`` method to crash (:ticket:`25686`). + +* Fixed a regression in 1.8.6 by restoring the ability to use ``Manager`` + objects for the ``queryset`` argument of ``ModelChoiceField`` + (:ticket:`25683`). diff --git a/tests/forms_tests/tests/tests.py b/tests/forms_tests/tests/tests.py index 6dafee1fb87..445f229e4af 100644 --- a/tests/forms_tests/tests/tests.py +++ b/tests/forms_tests/tests/tests.py @@ -60,17 +60,23 @@ class FileForm(Form): file1 = FileField() -class TestTicket12510(TestCase): - ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). ''' - def setUp(self): - self.groups = [Group.objects.create(name=name) for name in 'abc'] +class TestModelChoiceField(TestCase): def test_choices_not_fetched_when_not_rendering(self): + """ + Generating choices for ModelChoiceField should require 1 query (#12510). + """ + self.groups = [Group.objects.create(name=name) for name in 'abc'] # only one query is required to pull the model from DB with self.assertNumQueries(1): field = ModelChoiceField(Group.objects.order_by('-name')) self.assertEqual('a', field.clean(self.groups[0].pk).name) + def test_queryset_manager(self): + f = ModelChoiceField(ChoiceOptionModel.objects) + choice = ChoiceOptionModel.objects.create(name="choice 1") + self.assertEqual(list(f.choices), [('', '---------'), (choice.pk, str(choice))]) + class TestTicket14567(TestCase): """