Ticket #4653: form_for_instance_blank_option.diff

File form_for_instance_blank_option.diff, 4.5 KB (added by Ilya Semenov, 17 years ago)
  • django/db/models/fields/__init__.py

     
    390390        "Returns a django.newforms.Field instance for this database Field."
    391391        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
    392392        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)))
    394394        if self.has_default():
    395395            defaults['initial'] = self.get_default()
    396396        defaults.update(kwargs)
  • tests/modeltests/model_forms/models.py

     
    3030    (3, 'Live'),
    3131)
    3232
     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'),
     48)
     49
    3350class Category(models.Model):
    3451    name = models.CharField(max_length=20)
    3552    slug = models.SlugField(max_length=20)
     
    7087    def __unicode__(self):
    7188        return self.phone
    7289
     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.')
     95
    7396__test__ = {'API_TESTS': """
    7497>>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField
    7598>>> import datetime
     
    216239>>> print f
    217240<tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br />Use both first and last names.</td></tr>
    218241
     242Show the form for a new Car. Note that steering field doesn't include the blank choice,
     243because 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
     265Create a Car, and display the form for modifying it. Note that now the fuel
     266selector doesn't include the blank choice as well, since the field is
     267obligatory 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
    219290>>> art = Article(headline='Test article', slug='test-article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.')
    220291>>> art.save()
    221292>>> art.id
Back to Top