mirror of https://github.com/django/django.git
Fixed #25683 -- Allowed ModelChoiceField(queryset=...) to accept Managers.
This fixes a regression from refs #25496.
This commit is contained in:
parent
c6da4b0c68
commit
1155843a41
|
@ -1102,9 +1102,11 @@ class ModelChoiceIterator(object):
|
|||
def __iter__(self):
|
||||
if self.field.empty_label is not None:
|
||||
yield ("", self.field.empty_label)
|
||||
queryset = self.queryset.all()
|
||||
# Can't use iterator() when queryset uses prefetch_related()
|
||||
method = 'all' if self.queryset._prefetch_related_lookups else 'iterator'
|
||||
for obj in getattr(self.queryset, method)():
|
||||
if not queryset._prefetch_related_lookups:
|
||||
queryset = queryset.iterator()
|
||||
for obj in queryset:
|
||||
yield self.choice(obj)
|
||||
|
||||
def __len__(self):
|
||||
|
|
|
@ -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`).
|
||||
|
|
|
@ -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):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue