Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12215: Added len to ModelChoiceIterator. Thanks Alex and Tobias.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11850 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 125403ca972d4964691ee206acc6c28c0b3eab9d 1 parent 6a7db77
Karen Tracey authored December 13, 2009
3  django/forms/models.py
@@ -910,6 +910,9 @@ def __iter__(self):
910 910
             for obj in self.queryset.all():
911 911
                 yield self.choice(obj)
912 912
 
  913
+    def __len__(self):
  914
+        return len(self.queryset)
  915
+
913 916
     def choice(self, obj):
914 917
         if self.field.to_field_name:
915 918
             key = obj.serializable_value(self.field.to_field_name)
14  tests/regressiontests/model_forms_regress/tests.py
@@ -100,4 +100,16 @@ def test_save(self):
100 100
         # It's enough that the form saves without error -- the custom save routine will
101 101
         # generate an AssertionError if it is called more than once during save.
102 102
         form = CFFForm(data = {'f': None})
103  
-        form.save()
  103
+        form.save()
  104
+
  105
+class ModelChoiceIteratorTests(TestCase):
  106
+    def test_len(self):
  107
+        class Form(forms.ModelForm):
  108
+            class Meta:
  109
+                model = Article
  110
+                fields = ["publications"]
  111
+        
  112
+        Publication.objects.create(title="Pravda",
  113
+            date_published=date(1991, 8, 22))
  114
+        f = Form()
  115
+        self.assertEqual(len(f.fields["publications"].choices), 1)

0 notes on commit 125403c

Please sign in to comment.
Something went wrong with that request. Please try again.