Ticket #4653: form_for_instance_blank_option.diff
File form_for_instance_blank_option.diff, 4.5 KB (added by , 17 years ago) |
---|
-
TabularUnified django/db/models/fields/__init__.py
390 390 "Returns a django.newforms.Field instance for this database Field." 391 391 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 392 392 if self.choices: 393 defaults['widget'] = forms.Select(choices=self.get_choices( ))393 defaults['widget'] = forms.Select(choices=self.get_choices(include_blank=self.blank or not (self.has_default() or 'initial' in kwargs))) 394 394 if self.has_default(): 395 395 defaults['initial'] = self.get_default() 396 396 defaults.update(kwargs) -
TabularUnified 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 … … 216 239 >>> print f 217 240 <tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br />Use both first and last names.</td></tr> 218 241 242 Show the form for a new Car. Note that steering field doesn't include the blank choice, 243 because the field is obligatory and has an explicit default. 244 >>> CarForm = form_for_model(Car) 245 >>> f = CarForm(auto_id=False) 246 >>> print f 247 <tr><th>Name:</th><td><input type="text" name="name" maxlength="50" /></td></tr> 248 <tr><th>Steering:</th><td><select name="steering"> 249 <option value="left" selected="selected">Left steering wheel</option> 250 <option value="right">Right steering wheel</option> 251 </select></td></tr> 252 <tr><th>Fuel:</th><td><select name="fuel"> 253 <option value="" selected="selected">---------</option> 254 <option value="gas">Gasoline</option> 255 <option value="diesel">Diesel</option> 256 <option value="other">Other</option> 257 </select></td></tr> 258 <tr><th>Transmission:</th><td><select name="transmission"> 259 <option value="" selected="selected">---------</option> 260 <option value="at">Automatic</option> 261 <option value="mt">Manual</option> 262 <option value="cvt">CVT</option> 263 </select><br />Leave empty if not applicable.</td></tr> 264 265 Create a Car, and display the form for modifying it. Note that now the fuel 266 selector doesn't include the blank choice as well, since the field is 267 obligatory and can not be changed to be blank. 268 >>> honda = Car(name='Honda Accord Wagon', steering='right', fuel='gas', transmission='at') 269 >>> honda.save() 270 >>> HondaForm = form_for_instance(honda) 271 >>> f = HondaForm(auto_id=False) 272 >>> print f 273 <tr><th>Name:</th><td><input type="text" name="name" value="Honda Accord Wagon" maxlength="50" /></td></tr> 274 <tr><th>Steering:</th><td><select name="steering"> 275 <option value="left">Left steering wheel</option> 276 <option value="right" selected="selected">Right steering wheel</option> 277 </select></td></tr> 278 <tr><th>Fuel:</th><td><select name="fuel"> 279 <option value="gas" selected="selected">Gasoline</option> 280 <option value="diesel">Diesel</option> 281 <option value="other">Other</option> 282 </select></td></tr> 283 <tr><th>Transmission:</th><td><select name="transmission"> 284 <option value="">---------</option> 285 <option value="at" selected="selected">Automatic</option> 286 <option value="mt">Manual</option> 287 <option value="cvt">CVT</option> 288 </select><br />Leave empty if not applicable.</td></tr> 289 219 290 >>> art = Article(headline='Test article', slug='test-article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.') 220 291 >>> art.save() 221 292 >>> art.id