From d7e9bb0571a51e4c26e525c8539c510418f670d3 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Mon, 1 Sep 2008 19:20:03 +0000 Subject: [PATCH] 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 --- django/db/models/fields/__init__.py | 20 ++++++++++++++++---- tests/modeltests/model_forms/models.py | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index ce5ed343b0..4b8beb4341 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -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) diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 99680ff34c..252d46deac 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -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