Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #18172 -- Made models with __iter__ usable in ModelMultipleChoi…

…ceField

Thanks to Patryk Zawadzki for the patch.
  • Loading branch information...
commit 3989ce52ef78840eefe01541628daa220191c0ad 1 parent abd0f30
Patryk Zawadzki authored December 19, 2012 akaariai committed December 19, 2012
4  django/forms/models.py
@@ -1033,6 +1033,8 @@ def clean(self, value):
1033 1033
         return qs
1034 1034
 
1035 1035
     def prepare_value(self, value):
1036  
-        if hasattr(value, '__iter__') and not isinstance(value, six.text_type):
  1036
+        if (hasattr(value, '__iter__') and
  1037
+                not isinstance(value, six.text_type) and
  1038
+                not hasattr(value, '_meta')):
1037 1039
             return [super(ModelMultipleChoiceField, self).prepare_value(v) for v in value]
1038 1040
         return super(ModelMultipleChoiceField, self).prepare_value(value)
15  tests/modeltests/model_forms/models.py
@@ -263,3 +263,18 @@ class FlexibleDatePost(models.Model):
263 263
     slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
264 264
     subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
265 265
     posted = models.DateField(blank=True, null=True)
  266
+
  267
+@python_2_unicode_compatible
  268
+class Colour(models.Model):
  269
+    name = models.CharField(max_length=50)
  270
+
  271
+    def __iter__(self):
  272
+        for number in xrange(5):
  273
+            yield number
  274
+
  275
+    def __str__(self):
  276
+        return self.name
  277
+
  278
+class ColourfulItem(models.Model):
  279
+    name = models.CharField(max_length=50)
  280
+    colours = models.ManyToManyField(Colour)
16  tests/modeltests/model_forms/tests.py
@@ -19,7 +19,8 @@
19 19
     Category, CommaSeparatedInteger, CustomFieldForExclusionModel, DerivedBook,
20 20
     DerivedPost, ExplicitPK, FlexibleDatePost, ImprovedArticle,
21 21
     ImprovedArticleWithParentLink, Inventory, Post, Price,
22  
-    Product, TextFile, Writer, WriterProfile, test_images)
  22
+    Product, TextFile, Writer, WriterProfile, Colour, ColourfulItem,
  23
+    test_images)
23 24
 
24 25
 if test_images:
25 26
     from .models import ImageFile, OptionalImageFile
@@ -174,6 +175,10 @@ class Meta:
174 175
         model = Price
175 176
         exclude = ('quantity',)
176 177
 
  178
+class ColourfulItemForm(forms.ModelForm):
  179
+    class Meta:
  180
+        model = ColourfulItem
  181
+
177 182
 
178 183
 class ModelFormBaseTest(TestCase):
179 184
     def test_base_form(self):
@@ -1518,3 +1523,12 @@ def test_model_field_that_returns_none_to_exclude_itself_with_explicit_fields(se
1518 1523
                          ['name'])
1519 1524
         self.assertHTMLEqual(six.text_type(CustomFieldForExclusionForm()),
1520 1525
                          '''<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="10" /></td></tr>''')
  1526
+
  1527
+    def test_iterable_model_m2m(self) :
  1528
+        colour = Colour.objects.create(name='Blue')
  1529
+        form = ColourfulItemForm()
  1530
+        self.maxDiff = 1024
  1531
+        self.assertHTMLEqual(form.as_p(), """<p><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" maxlength="50" /></p>
  1532
+        <p><label for="id_colours">Colours:</label> <select multiple="multiple" name="colours" id="id_colours">
  1533
+        <option value="1">Blue</option>
  1534
+        </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")

0 notes on commit 3989ce5

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