Ticket #12215: modelchoice_iterator_len.3.diff

File modelchoice_iterator_len.3.diff, 1.4 KB (added by Tobias McNulty, 15 years ago)

same patch w/extra whitespace removed

  • django/forms/models.py

    diff -r eec2c68ffa03 django/forms/models.py
    a b  
    910910            for obj in self.queryset.all():
    911911                yield self.choice(obj)
    912912
     913    def __len__(self):
     914        return len(self.queryset)
     915
    913916    def choice(self, obj):
    914917        if self.field.to_field_name:
    915918            key = obj.serializable_value(self.field.to_field_name)
  • tests/regressiontests/model_forms_regress/tests.py

    diff -r eec2c68ffa03 tests/regressiontests/model_forms_regress/tests.py
    a b  
    100100        # It's enough that the form saves without error -- the custom save routine will
    101101        # generate an AssertionError if it is called more than once during save.
    102102        form = CFFForm(data = {'f': None})
    103         form.save()
    104  No newline at end of file
     103        form.save()
     104
     105
     106class ModelChoiceIteratorTests(TestCase):
     107    def test_len(self):
     108        class Form(forms.ModelForm):
     109            class Meta:
     110                model = Article
     111                fields = ["publications"]
     112       
     113        Publication.objects.create(title="Pravda",
     114            date_published=date(1991, 8, 22))
     115        f = Form()
     116        self.assertEqual(len(f.fields["publications"].choices), 1)
Back to Top