Django

Code

Changeset 6733

Show
Ignore:
Timestamp:
11/29/07 10:32:18 (7 months ago)
Author:
mtredinnick
Message:

Fixed #4653 -- Improved the logic to decide when to include (and select as
initial value) the blank choice for a model field with choices. Thanks to
Ilya Semenov for persisting with this.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r6731 r6733  
    274274    David Schein 
    275275    scott@staplefish.com 
     276    Ilya Semenov <semenov@inetss.com> 
    276277    serbaut@gmail.com 
    277278    John Shaffer <jshaffer2112@gmail.com> 
  • django/trunk/django/db/models/fields/__init__.py

    r6651 r6733  
    393393        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 
    394394        if self.choices: 
    395             defaults['widget'] = forms.Select(choices=self.get_choices()) 
     395            defaults['widget'] = forms.Select(choices=self.get_choices(include_blank=self.blank or not (self.has_default() or 'initial' in kwargs))) 
    396396        if self.has_default(): 
    397397            defaults['initial'] = self.get_default() 
  • django/trunk/docs/newforms.txt

    r6625 r6733  
    18501850    * If the model field has ``choices`` set, then the form field's ``widget`` 
    18511851      will be set to ``Select``, with choices coming from the model field's 
    1852       ``choices``. 
     1852      ``choices``. The choices will normally include the blank choice which is 
     1853      selected by default. If the field is required, this forces the user to 
     1854      make a selection. The blank choice will not be included if the model 
     1855      field has ``blank=False`` and an explicit ``default`` value (the 
     1856      ``default`` value will be initially selected instead). 
    18531857 
    18541858Finally, note that you can override the form field used for a given model 
     
    20962100    >>> f = AuthorForm() 
    20972101 
    2098 When a form created by ``form_for_instance()`` is created, the initial 
    2099 data values for the form fields are drawn from the instance. However, 
    2100 this data is not bound to the form. You will need to bind data to the 
    2101 form before the form can be saved. 
     2102When a form created by ``form_for_instance()`` is created, the initial data 
     2103values for the form fields are drawn from the instance. However, this data is 
     2104not bound to the form. You will need to bind data to the form before the form 
     2105can be saved. 
     2106 
     2107Unlike ``form_for_model()``, a choice field in form created by 
     2108``form_for_instance()`` will not include the blank choice if the respective 
     2109model field has ``blank=False``. The initial choice is drawn from the instance. 
    21022110 
    21032111When you call ``save()`` on a form created by ``form_for_instance()``, 
  • django/trunk/tests/modeltests/model_forms/models.py

    r6670 r6733  
    2929    (2, 'Pending'), 
    3030    (3, 'Live'), 
     31) 
     32 
     33STEERING_TYPE = ( 
     34    ('left', 'Left steering wheel'), 
     35    ('right', 'Right steering wheel'), 
     36) 
     37 
     38FUEL_TYPE = ( 
     39    ('gas', 'Gasoline'), 
     40    ('diesel', 'Diesel'), 
     41    ('other', 'Other'), 
     42) 
     43 
     44TRANSMISSION_TYPE = ( 
     45    ('at', 'Automatic'), 
     46    ('mt', 'Manual'), 
     47    ('cvt', 'CVT'), 
    3148) 
    3249 
     
    7087    def __unicode__(self): 
    7188        return self.phone 
     89 
     90class Car(models.Model): 
     91    name = models.CharField(max_length=50) 
     92    steering = models.CharField(max_length=5, choices=STEERING_TYPE, default='left') 
     93    fuel = models.CharField(max_length=10, choices=FUEL_TYPE) 
     94    transmission = models.CharField(max_length=3, choices=TRANSMISSION_TYPE, blank=True, help_text='Leave empty if not applicable.') 
    7295 
    7396__test__ = {'API_TESTS': """ 
     
    593616>>> f.cleaned_data 
    594617{'phone': u'312-555-1212', 'description': u'Assistance'} 
     618 
     619# form_for_* blank choices #################################################### 
     620 
     621Show the form for a new Car. Note that steering field doesn't include the blank choice, 
     622because the field is obligatory and has an explicit default. 
     623>>> CarForm = form_for_model(Car) 
     624>>> f = CarForm(auto_id=False) 
     625>>> print f 
     626<tr><th>Name:</th><td><input type="text" name="name" maxlength="50" /></td></tr> 
     627<tr><th>Steering:</th><td><select name="steering"> 
     628<option value="left" selected="selected">Left steering wheel</option> 
     629<option value="right">Right steering wheel</option> 
     630</select></td></tr> 
     631<tr><th>Fuel:</th><td><select name="fuel"> 
     632<option value="" selected="selected">---------</option> 
     633<option value="gas">Gasoline</option> 
     634<option value="diesel">Diesel</option> 
     635<option value="other">Other</option> 
     636</select></td></tr> 
     637<tr><th>Transmission:</th><td><select name="transmission"> 
     638<option value="" selected="selected">---------</option> 
     639<option value="at">Automatic</option> 
     640<option value="mt">Manual</option> 
     641<option value="cvt">CVT</option> 
     642</select><br />Leave empty if not applicable.</td></tr> 
     643 
     644Create a Car, and display the form for modifying it. Note that now the fuel 
     645selector doesn't include the blank choice as well, since the field is 
     646obligatory and can not be changed to be blank. 
     647>>> honda = Car(name='Honda Accord Wagon', steering='right', fuel='gas', transmission='at') 
     648>>> honda.save() 
     649>>> HondaForm = form_for_instance(honda) 
     650>>> f = HondaForm(auto_id=False) 
     651>>> print f 
     652<tr><th>Name:</th><td><input type="text" name="name" value="Honda Accord Wagon" maxlength="50" /></td></tr> 
     653<tr><th>Steering:</th><td><select name="steering"> 
     654<option value="left">Left steering wheel</option> 
     655<option value="right" selected="selected">Right steering wheel</option> 
     656</select></td></tr> 
     657<tr><th>Fuel:</th><td><select name="fuel"> 
     658<option value="gas" selected="selected">Gasoline</option> 
     659<option value="diesel">Diesel</option> 
     660<option value="other">Other</option> 
     661</select></td></tr> 
     662<tr><th>Transmission:</th><td><select name="transmission"> 
     663<option value="">---------</option> 
     664<option value="at" selected="selected">Automatic</option> 
     665<option value="mt">Manual</option> 
     666<option value="cvt">CVT</option> 
     667</select><br />Leave empty if not applicable.</td></tr> 
    595668"""}