Ticket #4653: form_for_instance_blank_option-r2.diff
File form_for_instance_blank_option-r2.diff, 6.1 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/__init__.py
392 392 "Returns a django.newforms.Field instance for this database Field." 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() 398 398 defaults.update(kwargs) -
tests/modeltests/model_forms/models.py
30 30 (3, 'Live'), 31 31 ) 32 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'), 48 ) 49 33 50 class Category(models.Model): 34 51 name = models.CharField(max_length=20) 35 52 slug = models.SlugField(max_length=20) … … 70 87 def __unicode__(self): 71 88 return self.phone 72 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.') 95 73 96 __test__ = {'API_TESTS': """ 74 97 >>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField 75 98 >>> import datetime … … 592 615 True 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 """} -
AUTHORS
272 272 Vinay Sajip <vinay_sajip@yahoo.co.uk> 273 273 David Schein 274 274 scott@staplefish.com 275 Ilya Semenov <semenov@inetss.com> 275 276 serbaut@gmail.com 276 277 John Shaffer <jshaffer2112@gmail.com> 277 278 Pete Shinners <pete@shinners.org> -
docs/newforms.txt
1849 1849 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 include the blank choice which is selected 1853 by default. If the field is required, that effectively forces a user to 1854 make a selection. The blank choice will not be included if the model 1855 field has ``blank=False`` and explicit ``default`` value. 1853 1856 1854 1857 Finally, note that you can override the form field used for a given model 1855 1858 field. See "Overriding the default field types" below. … … 2100 2103 this data is not bound to the form. You will need to bind data to the 2101 2104 form before the form can be saved. 2102 2105 2106 Unlike ``form_for_model()``, choice fields in form created by 2107 ``form_for_instance()`` will have a blank choice only when the respective model 2108 field has ``blank=True``. The default choice is drawn from the instance. 2109 2103 2110 When you call ``save()`` on a form created by ``form_for_instance()``, 2104 2111 the database instance will be updated. As in ``form_for_model()``, ``save()`` 2105 2112 will raise ``ValueError`` if the data doesn't validate.