Ticket #4237: radio_select_with_tests.diff

File radio_select_with_tests.diff, 5.7 KB (added by Robert Myers <myer0052@…>, 17 years ago)

Patch now with tests and documentation.

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

     
    349349        "Returns a django.newforms.Field instance for this database Field."
    350350        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
    351351        if self.choices:
    352             defaults['widget'] = forms.Select(choices=self.get_choices())
     352            if self.radio_admin:
     353                defaults['widget'] = forms.RadioSelect(choices=self.get_choices_default())
     354            else:
     355                defaults['widget'] = forms.Select(choices=self.get_choices_default())
    353356        defaults.update(kwargs)
    354357        return form_class(**defaults)
    355358
  • docs/newforms.txt

     
    13101310
    13111311    * If the model field has ``choices`` set, then the form field's ``widget``
    13121312      will be set to ``Select``, with choices coming from the model field's
    1313       ``choices``.
     1313      ``choices``. If the model field specifies ``radio_admin=True`` then the
     1314      form field's ``widget`` will be ``RadioSelect`` instead.
    13141315
    13151316Finally, note that you can override the form field used for a given model
    13161317field. See "Overriding the default field types" below.
  • tests/modeltests/model_forms/models.py

     
    5151    article = models.TextField()
    5252    categories = models.ManyToManyField(Category, blank=True)
    5353    status = models.IntegerField(choices=ARTICLE_STATUS, blank=True, null=True)
     54    radio_status = models.IntegerField(choices=ARTICLE_STATUS, radio_admin=True, blank=True, null=True)
    5455
    5556    def save(self):
    5657        import datetime
     
    155156>>> w.save()
    156157
    157158ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any
    158 fields with the 'choices' attribute are represented by a ChoiceField.
     159fields with the 'choices' attribute are represented by a ChoiceField. Fields
     160with a 'choices' attribute and 'radio_admin' set to True will use a RadioSelect
     161widget.
    159162>>> ArticleForm = form_for_model(Article)
    160163>>> f = ArticleForm(auto_id=False)
    161164>>> print f
     
    173176<option value="2">Pending</option>
    174177<option value="3">Live</option>
    175178</select></td></tr>
     179<tr><th>Radio status:</th><td><ul>
     180<li><label><input checked="checked" type="radio" name="radio_status" value="" /> None</label></li>
     181<li><label><input type="radio" name="radio_status" value="1" /> Draft</label></li>
     182<li><label><input type="radio" name="radio_status" value="2" /> Pending</label></li>
     183<li><label><input type="radio" name="radio_status" value="3" /> Live</label></li>
     184</ul></td></tr>
    176185<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
    177186<option value="1">Entertainment</option>
    178187<option value="2">It&#39;s a test</option>
     
    231240<option value="2">Pending</option>
    232241<option value="3">Live</option>
    233242</select></li>
     243<li>Radio status: <ul>
     244<li><label><input checked="checked" type="radio" name="radio_status" value="" /> None</label></li>
     245<li><label><input type="radio" name="radio_status" value="1" /> Draft</label></li>
     246<li><label><input type="radio" name="radio_status" value="2" /> Pending</label></li>
     247<li><label><input type="radio" name="radio_status" value="3" /> Live</label></li>
     248</ul></li>
    234249<li>Categories: <select multiple="multiple" name="categories">
    235250<option value="1">Entertainment</option>
    236251<option value="2">It&#39;s a test</option>
     
    285300<option value="2">Pending</option>
    286301<option value="3">Live</option>
    287302</select></li>
     303<li>Radio status: <ul>
     304<li><label><input checked="checked" type="radio" name="radio_status" value="" /> None</label></li>
     305<li><label><input type="radio" name="radio_status" value="1" /> Draft</label></li>
     306<li><label><input type="radio" name="radio_status" value="2" /> Pending</label></li>
     307<li><label><input type="radio" name="radio_status" value="3" /> Live</label></li>
     308</ul></li>
    288309<li>Categories: <select multiple="multiple" name="categories">
    289310<option value="1" selected="selected">Entertainment</option>
    290311<option value="2">It&#39;s a test</option>
     
    369390<option value="2">Pending</option>
    370391<option value="3">Live</option>
    371392</select></li>
     393<li>Radio status: <ul>
     394<li><label><input checked="checked" type="radio" name="radio_status" value="" /> None</label></li>
     395<li><label><input type="radio" name="radio_status" value="1" /> Draft</label></li>
     396<li><label><input type="radio" name="radio_status" value="2" /> Pending</label></li>
     397<li><label><input type="radio" name="radio_status" value="3" /> Live</label></li>
     398</ul></li>
    372399<li>Categories: <select multiple="multiple" name="categories">
    373400<option value="1">Entertainment</option>
    374401<option value="2">It&#39;s a test</option>
     
    394421<option value="2">Pending</option>
    395422<option value="3">Live</option>
    396423</select></li>
     424<li>Radio status: <ul>
     425<li><label><input checked="checked" type="radio" name="radio_status" value="" /> None</label></li>
     426<li><label><input type="radio" name="radio_status" value="1" /> Draft</label></li>
     427<li><label><input type="radio" name="radio_status" value="2" /> Pending</label></li>
     428<li><label><input type="radio" name="radio_status" value="3" /> Live</label></li>
     429</ul></li>
    397430<li>Categories: <select multiple="multiple" name="categories">
    398431<option value="1">Entertainment</option>
    399432<option value="2">It&#39;s a test</option>
Back to Top