Changeset 8772
- Timestamp:
- 08/31/08 15:11:11 (3 months ago)
- Files:
-
- django/trunk/django/db/models/fields/__init__.py (modified) (1 diff)
- django/trunk/tests/modeltests/model_forms/models.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/db/models/fields/__init__.py
r8691 r8772 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() django/trunk/tests/modeltests/model_forms/models.py
r8756 r8772 26 26 (2, 'Pending'), 27 27 (3, 'Live'), 28 ) 29 30 ARTICLE_STATUS_CHAR = ( 31 ('d', 'Draft'), 32 ('p', 'Pending'), 33 ('l', 'Live'), 28 34 ) 29 35 … … 111 117 def __unicode__(self): 112 118 return self.field 119 120 class ArticleStatus(models.Model): 121 status = models.CharField(max_length=2, choices=ARTICLE_STATUS_CHAR, blank=True, null=True) 113 122 114 123 __test__ = {'API_TESTS': """ … … 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 """}
