diff --git a/django/forms/models.py b/django/forms/models.py index 61ff8e4dcc..3f7621e5e7 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -910,6 +910,9 @@ class ModelChoiceIterator(object): for obj in self.queryset.all(): yield self.choice(obj) + def __len__(self): + return len(self.queryset) + def choice(self, obj): if self.field.to_field_name: key = obj.serializable_value(self.field.to_field_name) diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py index 85e284b639..f8b6511140 100644 --- a/tests/regressiontests/model_forms_regress/tests.py +++ b/tests/regressiontests/model_forms_regress/tests.py @@ -100,4 +100,16 @@ class CustomFieldSaveTests(TestCase): # It's enough that the form saves without error -- the custom save routine will # generate an AssertionError if it is called more than once during save. form = CFFForm(data = {'f': None}) - form.save() \ No newline at end of file + form.save() + +class ModelChoiceIteratorTests(TestCase): + def test_len(self): + class Form(forms.ModelForm): + class Meta: + model = Article + fields = ["publications"] + + Publication.objects.create(title="Pravda", + date_published=date(1991, 8, 22)) + f = Form() + self.assertEqual(len(f.fields["publications"].choices), 1)