diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index a074e2bdab..3849ee73b9 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -344,6 +344,8 @@ class Field(object): def formfield(self, form_class=forms.CharField, **kwargs): "Returns a django.newforms.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()) defaults.update(kwargs) return form_class(**defaults) diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 2757787571..a23529b566 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -24,6 +24,12 @@ a commit=True parameter. from django.db import models +ARTICLE_STATUS = ( + (1, 'Draft'), + (2, 'Pending'), + (3, 'Live'), +) + class Category(models.Model): name = models.CharField(maxlength=20) url = models.CharField('The URL', maxlength=40) @@ -44,6 +50,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) def save(self): import datetime @@ -147,8 +154,8 @@ Create a couple of Writers. >>> w = Writer(name='Bob Woodward') >>> w.save() -ManyToManyFields are represented by a MultipleChoiceField, and ForeignKeys are -represented by a ChoiceField. +ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any +fields with the 'choices' attribute are represented by a ChoiceField. >>> ArticleForm = form_for_model(Article) >>> f = ArticleForm(auto_id=False) >>> print f @@ -160,6 +167,12 @@ represented by a ChoiceField. Article: +Status: Categories:
  • Article:
  • +
  • Status:
  • Categories:
  • Article:
  • +
  • Status:
  • Categories:
  • Article:
  • +
  • Status:
  • Categories:
  • Article:
  • +
  • Status:
  • Categories: