Permalink
Browse files

Repaired an oversight from [8772] that let made certain types of fiel…

…ds with choices fail. Fixes #6967 again.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8806 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent ea05e61 commit d7e9bb0571a51e4c26e525c8539c510418f670d3 @jacobian jacobian committed Sep 1, 2008
Showing with 17 additions and 5 deletions.
  1. +16 −4 django/db/models/fields/__init__.py
  2. +1 −1 tests/modeltests/model_forms/models.py
@@ -304,16 +304,28 @@ def save_form_data(self, instance, data):
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

0 comments on commit d7e9bb0

Please sign in to comment.