mirror of
https://github.com/django/django.git
synced 2024-11-29 22:56:46 +01:00
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
This commit is contained in:
parent
ea05e61b2b
commit
d7e9bb0571
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user