Ticket #3268: select_widget_for_fields_with_choices.diff

File select_widget_for_fields_with_choices.diff, 8.0 KB (added by Tai Lee, 18 years ago)

updated *Field.formfield() methods, with tests.

  • django/db/models/fields/__init__.py

     
    344344    def formfield(self, **kwargs):
    345345        "Returns a django.newforms.Field instance for this database Field."
    346346        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     347        if self.choices:
     348            defaults.update({'widget': forms.Select(choices=self.get_choices())})
    347349        defaults.update(kwargs)
    348350        return forms.CharField(**defaults)
    349351
     
    406408
    407409    def formfield(self, **kwargs):
    408410        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     411        if self.choices:
     412            defaults.update({'widget': forms.Select(choices=self.get_choices())})
    409413        defaults.update(kwargs)
    410414        return forms.BooleanField(**defaults)
    411415
     
    425429
    426430    def formfield(self, **kwargs):
    427431        defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     432        if self.choices:
     433            defaults.update({'widget': forms.Select(choices=self.get_choices())})
    428434        defaults.update(kwargs)
    429435        return forms.CharField(**defaults)
    430436
     
    503509
    504510    def formfield(self, **kwargs):
    505511        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     512        if self.choices:
     513            defaults.update({'widget': forms.Select(choices=self.get_choices())})
    506514        defaults.update(kwargs)
    507515        return forms.DateField(**defaults)
    508516
     
    568576
    569577    def formfield(self, **kwargs):
    570578        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     579        if self.choices:
     580            defaults.update({'widget': forms.Select(choices=self.get_choices())})
    571581        defaults.update(kwargs)
    572582        return forms.DateTimeField(**defaults)
    573583
     
    587597
    588598    def formfield(self, **kwargs):
    589599        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     600        if self.choices:
     601            defaults.update({'widget': forms.Select(choices=self.get_choices())})
    590602        defaults.update(kwargs)
    591603        return forms.EmailField(**defaults)
    592604
     
    724736
    725737    def formfield(self, **kwargs):
    726738        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     739        if self.choices:
     740            defaults.update({'widget': forms.Select(choices=self.get_choices())})
    727741        defaults.update(kwargs)
    728742        return forms.IntegerField(**defaults)
    729743
     
    763777    def formfield(self, **kwargs):
    764778        from django.contrib.localflavor.usa.forms import USPhoneNumberField
    765779        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     780        if self.choices:
     781            defaults.update({'widget': forms.Select(choices=self.get_choices())})
    766782        defaults.update(kwargs)
    767783        return USPhoneNumberField(**defaults)
    768784
     
    796812
    797813    def formfield(self, **kwargs):
    798814        defaults = {'required': not self.blank, 'widget': forms.Textarea, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     815        if self.choices:
     816            defaults.update({'widget': forms.Select(choices=self.get_choices())})
    799817        defaults.update(kwargs)
    800818        return forms.CharField(**defaults)
    801819
     
    841859
    842860    def formfield(self, **kwargs):
    843861        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     862        if self.choices:
     863            defaults.update({'widget': forms.Select(choices=self.get_choices())})
    844864        defaults.update(kwargs)
    845865        return forms.TimeField(**defaults)
    846866
     
    860880
    861881    def formfield(self, **kwargs):
    862882        defaults = {'required': not self.blank, 'verify_exists': self.verify_exists, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     883        if self.choices:
     884            defaults.update({'widget': forms.Select(choices=self.get_choices())})
    863885        defaults.update(kwargs)
    864886        return forms.URLField(**defaults)
    865887
  • tests/modeltests/model_forms/models.py

     
    2424
    2525from django.db import models
    2626
     27ARTICLE_STATUS_CHOICES = (
     28    (1, 'Draft'),
     29    (2, 'Pending'),
     30    (3, 'Live'),
     31)
     32
    2733class Category(models.Model):
    2834    name = models.CharField(maxlength=20)
    2935    url = models.CharField('The URL', maxlength=40)
     
    4450    writer = models.ForeignKey(Writer)
    4551    article = models.TextField()
    4652    categories = models.ManyToManyField(Category, blank=True)
     53    status = models.IntegerField(blank=True, choices=ARTICLE_STATUS_CHOICES, null=True)
    4754
    4855    def save(self):
    4956        import datetime
     
    147154>>> w = Writer(name='Bob Woodward')
    148155>>> w.save()
    149156
    150 ManyToManyFields are represented by a MultipleChoiceField, and ForeignKeys are
    151 represented by a ChoiceField.
     157ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any
     158fields with choices defined are represented by a ChoiceField.
    152159>>> ArticleForm = form_for_model(Article)
    153160>>> f = ArticleForm(auto_id=False)
    154161>>> print f
     
    160167<option value="2">Bob Woodward</option>
    161168</select></td></tr>
    162169<tr><th>Article:</th><td><textarea name="article"></textarea></td></tr>
     170<tr><th>Status:</th><td><select name="status">
     171<option value="" selected="selected">---------</option>
     172<option value="1">Draft</option>
     173<option value="2">Pending</option>
     174<option value="3">Live</option>
     175</select></td></tr>
    163176<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
    164177<option value="1">Entertainment</option>
    165178<option value="2">It&#39;s a test</option>
     
    200213<option value="2">Bob Woodward</option>
    201214</select></li>
    202215<li>Article: <textarea name="article">Hello.</textarea></li>
     216<li>Status: <select name="status">
     217<option value="" selected="selected">---------</option>
     218<option value="1">Draft</option>
     219<option value="2">Pending</option>
     220<option value="3">Live</option>
     221</select></li>
    203222<li>Categories: <select multiple="multiple" name="categories">
    204223<option value="1">Entertainment</option>
    205224<option value="2">It&#39;s a test</option>
     
    232251<option value="2">Bob Woodward</option>
    233252</select></li>
    234253<li>Article: <textarea name="article">Hello.</textarea></li>
     254<li>Status: <select name="status">
     255<option value="" selected="selected">---------</option>
     256<option value="1">Draft</option>
     257<option value="2">Pending</option>
     258<option value="3">Live</option>
     259</select></li>
    235260<li>Categories: <select multiple="multiple" name="categories">
    236261<option value="1" selected="selected">Entertainment</option>
    237262<option value="2">It&#39;s a test</option>
     
    310335<option value="2">Bob Woodward</option>
    311336</select></li>
    312337<li>Article: <textarea name="article"></textarea></li>
     338<li>Status: <select name="status">
     339<option value="" selected="selected">---------</option>
     340<option value="1">Draft</option>
     341<option value="2">Pending</option>
     342<option value="3">Live</option>
     343</select></li>
    313344<li>Categories: <select multiple="multiple" name="categories">
    314345<option value="1">Entertainment</option>
    315346<option value="2">It&#39;s a test</option>
     
    329360<option value="3">Carl Bernstein</option>
    330361</select></li>
    331362<li>Article: <textarea name="article"></textarea></li>
     363<li>Status: <select name="status">
     364<option value="" selected="selected">---------</option>
     365<option value="1">Draft</option>
     366<option value="2">Pending</option>
     367<option value="3">Live</option>
     368</select></li>
    332369<li>Categories: <select multiple="multiple" name="categories">
    333370<option value="1">Entertainment</option>
    334371<option value="2">It&#39;s a test</option>
Back to Top