Commit d7e9bb05 authored by Jacob Kaplan-Moss's avatar Jacob Kaplan-Moss
Browse files

Repaired an oversight from [8772] that let made certain types of fields with...

Repaired an oversight from [8772] that let made certain types of fields with choices fail. Fixes #6967 again.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8806 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent ea05e61b
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -304,16 +304,28 @@ class Field(object):
    def formfield(self, form_class=forms.CharField, **kwargs):
        "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.has_default():
            defaults['initial'] = self.get_default()

        if self.choices:
            form_class = forms.TypedChoiceField
            # Fields with choices get special treatment. 
            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()
            
            form_class = forms.TypedChoiceField
            
            # Many of the subclass-specific formfield arguments (min_value,
            # max_value) don't apply for choice fields, so be sure to only pass
            # the values that TypedChoiceField will understand.
            for k in kwargs.keys():
                if k not in ('coerce', 'empty_value', 'choices', 'required',
                             'widget', 'label', 'initial', 'help_text',
                             'error_messages'):
                    del kwargs[k]
        
        defaults.update(kwargs)
        return form_class(**defaults)

+1 −1
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ class Article(models.Model):
    writer = models.ForeignKey(Writer)
    article = models.TextField()
    categories = models.ManyToManyField(Category, blank=True)
    status = models.IntegerField(choices=ARTICLE_STATUS, blank=True, null=True)
    status = models.PositiveIntegerField(choices=ARTICLE_STATUS, blank=True, null=True)

    def save(self):
        import datetime