Loading django/forms/models.py +3 −0 Original line number Diff line number Diff line Loading @@ -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) Loading tests/regressiontests/model_forms_regress/tests.py +13 −1 Original line number Diff line number Diff line Loading @@ -101,3 +101,15 @@ class CustomFieldSaveTests(TestCase): # generate an AssertionError if it is called more than once during save. form = CFFForm(data = {'f': None}) 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) Loading
django/forms/models.py +3 −0 Original line number Diff line number Diff line Loading @@ -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) Loading
tests/regressiontests/model_forms_regress/tests.py +13 −1 Original line number Diff line number Diff line Loading @@ -101,3 +101,15 @@ class CustomFieldSaveTests(TestCase): # generate an AssertionError if it is called more than once during save. form = CFFForm(data = {'f': None}) 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)