Fixed #6967: `ModelForm`s now validate choices. Thanks, mattmcc -- the failing test helped quite a bit.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8772 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2008-08-31 20:11:11 +00:00
parent 4ae746b574
commit 3b63953704
2 changed files with 36 additions and 1 deletions

View File

@ -305,7 +305,13 @@ class Field(object):
"Returns a django.forms.Field instance for this database Field."
defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
if self.choices:
defaults['widget'] = forms.Select(choices=self.get_choices(include_blank=self.blank or not (self.has_default() or 'initial' in kwargs)))
form_class = forms.TypedChoiceField
include_blank = self.blank or not (self.has_default() or 'initial' in kwargs)
defaults['choices'] = self.get_choices(include_blank=include_blank)
defaults['coerce'] = self.to_python
if self.null:
defaults['empty_value'] = None
kwargs.pop('max_length', None)
if self.has_default():
defaults['initial'] = self.get_default()
defaults.update(kwargs)

View File

@ -27,6 +27,12 @@ ARTICLE_STATUS = (
(3, 'Live'),
)
ARTICLE_STATUS_CHAR = (
('d', 'Draft'),
('p', 'Pending'),
('l', 'Live'),
)
class Category(models.Model):
name = models.CharField(max_length=20)
slug = models.SlugField(max_length=20)
@ -111,6 +117,9 @@ class CommaSeparatedInteger(models.Model):
def __unicode__(self):
return self.field
class ArticleStatus(models.Model):
status = models.CharField(max_length=2, choices=ARTICLE_STATUS_CHAR, blank=True, null=True)
__test__ = {'API_TESTS': """
>>> from django import forms
>>> from django.forms.models import ModelForm, model_to_dict
@ -1123,4 +1132,24 @@ u'1,,2'
>>> f.clean('1')
u'1'
# Choices on CharField and IntegerField
>>> class ArticleForm(ModelForm):
... class Meta:
... model = Article
>>> f = ArticleForm()
>>> f.fields['status'].clean('42')
Traceback (most recent call last):
...
ValidationError: [u'Select a valid choice. 42 is not one of the available choices.']
>>> class ArticleStatusForm(ModelForm):
... class Meta:
... model = ArticleStatus
>>> f = ArticleStatusForm()
>>> f.fields['status'].clean('z')
Traceback (most recent call last):
...
ValidationError: [u'Select a valid choice. z is not one of the available choices.']
"""}