tickets: 8663
This data as json
id | created | changetime | last_pulled_from_trac | stage | status | component | type | severity | version | resolution | summary | description | owner | reporter | keywords | easy | has_patch | needs_better_patch | needs_tests | needs_docs | ui_ux |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
8663 | 2008-08-29 01:07:00 | 2011-09-28 16:12:17 | 2022-03-06 03:42:50.857469 | Design decision needed | closed | Forms | dev | wontfix | Inconsistencies/Bug in ModelForm | When a ModelForm is used to display a form for a Model, the fields defined with a choices option insert a "-------" value for the first option when the form is rendered. If you override a field and manually specify the choices for a Select widget, this "-------" does not appear as the first choice. {{{ # models.py from django.db import models MY_CHOICES = ( (0, 'Zero'), (1, 'One'), ) class MyModel(models.Model): my_field = models.IntegerField(choices=MY_CHOICES) }}} {{{ # forms.py from django import forms from myapp.models import MyModel, MY_CHOICES class MyModelForm(forms.ModelForm): #my_field = forms.IntegerField(widget=forms.Select(choices=MY_CHOICES)) class Meta: model = MyModel }}} View the HTML for the form with my_field commented out: {{{ >>> from myapp.forms import MyModelForm >>> f = MyModelForm() >>> print f <tr><th><label for="id_my_field">My field:</label></th><td><select name="my_field" id="id_my_field"> <option value="" selected="selected">---------</option> <option value="0">Zero</option> <option value="1">One</option> </select></td></tr> }}} Now uncomment my_field in MyModelForm: {{{ >>> from myapp.forms import MyModelForm >>> f = MyModelForm() >>> print f <tr><th><label for="id_my_field">My field:</label></th><td><select name="my_field" id="id_my_field"> <option value="0">Zero</option> <option value="1">One</option> </select></td></tr> }}} This value doesn't appear in the 2nd case: <option value="" selected="selected">---------</option> SVN-8643 | nobody | lingrlongr | ModelForm forms | 0 | 0 | 0 | 0 | 0 | 0 |