Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6967: `ModelForm`s now validate choices. Thanks, mattmcc -- th…

…e failing test helped quite a bit.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8772 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3b6395370480f44f180c6287e70ea569cd20f0d4 1 parent 4ae746b
Jacob Kaplan-Moss authored August 31, 2008
8  django/db/models/fields/__init__.py
@@ -305,7 +305,13 @@ def formfield(self, form_class=forms.CharField, **kwargs):
305 305
         "Returns a django.forms.Field instance for this database Field."
306 306
         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
307 307
         if self.choices:
308  
-            defaults['widget'] = forms.Select(choices=self.get_choices(include_blank=self.blank or not (self.has_default() or 'initial' in kwargs)))
  308
+            form_class = forms.TypedChoiceField
  309
+            include_blank = self.blank or not (self.has_default() or 'initial' in kwargs)
  310
+            defaults['choices'] = self.get_choices(include_blank=include_blank)
  311
+            defaults['coerce'] = self.to_python
  312
+            if self.null:
  313
+                defaults['empty_value'] = None
  314
+            kwargs.pop('max_length', None)
309 315
         if self.has_default():
310 316
             defaults['initial'] = self.get_default()
311 317
         defaults.update(kwargs)
29  tests/modeltests/model_forms/models.py
@@ -27,6 +27,12 @@
27 27
     (3, 'Live'),
28 28
 )
29 29
 
  30
+ARTICLE_STATUS_CHAR = (
  31
+    ('d', 'Draft'),
  32
+    ('p', 'Pending'),
  33
+    ('l', 'Live'),
  34
+)
  35
+
30 36
 class Category(models.Model):
31 37
     name = models.CharField(max_length=20)
32 38
     slug = models.SlugField(max_length=20)
@@ -111,6 +117,9 @@ class CommaSeparatedInteger(models.Model):
111 117
     def __unicode__(self):
112 118
         return self.field
113 119
 
  120
+class ArticleStatus(models.Model):
  121
+    status = models.CharField(max_length=2, choices=ARTICLE_STATUS_CHAR, blank=True, null=True)
  122
+
114 123
 __test__ = {'API_TESTS': """
115 124
 >>> from django import forms
116 125
 >>> from django.forms.models import ModelForm, model_to_dict
@@ -1123,4 +1132,24 @@ def __unicode__(self):
1123 1132
 >>> f.clean('1')
1124 1133
 u'1'
1125 1134
 
  1135
+# Choices on CharField and IntegerField
  1136
+
  1137
+>>> class ArticleForm(ModelForm):
  1138
+...     class Meta:
  1139
+...         model = Article
  1140
+>>> f = ArticleForm()
  1141
+>>> f.fields['status'].clean('42')
  1142
+Traceback (most recent call last):
  1143
+...
  1144
+ValidationError: [u'Select a valid choice. 42 is not one of the available choices.']
  1145
+
  1146
+>>> class ArticleStatusForm(ModelForm):
  1147
+...     class Meta:
  1148
+...         model = ArticleStatus
  1149
+>>> f = ArticleStatusForm()
  1150
+>>> f.fields['status'].clean('z')
  1151
+Traceback (most recent call last):
  1152
+...
  1153
+ValidationError: [u'Select a valid choice. z is not one of the available choices.']
  1154
+
1126 1155
 """}

0 notes on commit 3b63953

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