Changeset 6733
- Timestamp:
- 11/29/07 10:32:18 (7 months ago)
- Files:
-
- django/trunk/AUTHORS (modified) (1 diff)
- django/trunk/django/db/models/fields/__init__.py (modified) (1 diff)
- django/trunk/docs/newforms.txt (modified) (2 diffs)
- django/trunk/tests/modeltests/model_forms/models.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/AUTHORS
r6731 r6733 274 274 David Schein 275 275 scott@staplefish.com 276 Ilya Semenov <semenov@inetss.com> 276 277 serbaut@gmail.com 277 278 John Shaffer <jshaffer2112@gmail.com> django/trunk/django/db/models/fields/__init__.py
r6651 r6733 393 393 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 394 394 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))) 396 396 if self.has_default(): 397 397 defaults['initial'] = self.get_default() django/trunk/docs/newforms.txt
r6625 r6733 1850 1850 * If the model field has ``choices`` set, then the form field's ``widget`` 1851 1851 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). 1853 1857 1854 1858 Finally, note that you can override the form field used for a given model … … 2096 2100 >>> f = AuthorForm() 2097 2101 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. 2102 When a form created by ``form_for_instance()`` is created, the initial data 2103 values for the form fields are drawn from the instance. However, this data is 2104 not bound to the form. You will need to bind data to the form before the form 2105 can be saved. 2106 2107 Unlike ``form_for_model()``, a choice field in form created by 2108 ``form_for_instance()`` will not include the blank choice if the respective 2109 model field has ``blank=False``. The initial choice is drawn from the instance. 2102 2110 2103 2111 When you call ``save()`` on a form created by ``form_for_instance()``, django/trunk/tests/modeltests/model_forms/models.py
r6670 r6733 29 29 (2, 'Pending'), 30 30 (3, 'Live'), 31 ) 32 33 STEERING_TYPE = ( 34 ('left', 'Left steering wheel'), 35 ('right', 'Right steering wheel'), 36 ) 37 38 FUEL_TYPE = ( 39 ('gas', 'Gasoline'), 40 ('diesel', 'Diesel'), 41 ('other', 'Other'), 42 ) 43 44 TRANSMISSION_TYPE = ( 45 ('at', 'Automatic'), 46 ('mt', 'Manual'), 47 ('cvt', 'CVT'), 31 48 ) 32 49 … … 70 87 def __unicode__(self): 71 88 return self.phone 89 90 class 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.') 72 95 73 96 __test__ = {'API_TESTS': """ … … 593 616 >>> f.cleaned_data 594 617 {'phone': u'312-555-1212', 'description': u'Assistance'} 618 619 # form_for_* blank choices #################################################### 620 621 Show the form for a new Car. Note that steering field doesn't include the blank choice, 622 because 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 644 Create a Car, and display the form for modifying it. Note that now the fuel 645 selector doesn't include the blank choice as well, since the field is 646 obligatory 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> 595 668 """}
