Ticket #4653: form_for_instance_blank_option-r1.diff

File form_for_instance_blank_option-r1.diff, 4.4 KB (added by Ilya Semenov, 17 years ago)

Moved new tests to separate section at the end of file

  • 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
     
    560583True
    561584>>> f.cleaned_data
    562585{'phone': u'312-555-1212', 'description': u'Assistance'}
     586
     587# form_for_* blank choices ####################################################
     588
     589Show the form for a new Car. Note that steering field doesn't include the blank choice,
     590because the field is obligatory and has an explicit default.
     591>>> CarForm = form_for_model(Car)
     592>>> f = CarForm(auto_id=False)
     593>>> print f
     594<tr><th>Name:</th><td><input type="text" name="name" maxlength="50" /></td></tr>
     595<tr><th>Steering:</th><td><select name="steering">
     596<option value="left" selected="selected">Left steering wheel</option>
     597<option value="right">Right steering wheel</option>
     598</select></td></tr>
     599<tr><th>Fuel:</th><td><select name="fuel">
     600<option value="" selected="selected">---------</option>
     601<option value="gas">Gasoline</option>
     602<option value="diesel">Diesel</option>
     603<option value="other">Other</option>
     604</select></td></tr>
     605<tr><th>Transmission:</th><td><select name="transmission">
     606<option value="" selected="selected">---------</option>
     607<option value="at">Automatic</option>
     608<option value="mt">Manual</option>
     609<option value="cvt">CVT</option>
     610</select><br />Leave empty if not applicable.</td></tr>
     611
     612Create a Car, and display the form for modifying it. Note that now the fuel
     613selector doesn't include the blank choice as well, since the field is
     614obligatory and can not be changed to be blank.
     615>>> honda = Car(name='Honda Accord Wagon', steering='right', fuel='gas', transmission='at')
     616>>> honda.save()
     617>>> HondaForm = form_for_instance(honda)
     618>>> f = HondaForm(auto_id=False)
     619>>> print f
     620<tr><th>Name:</th><td><input type="text" name="name" value="Honda Accord Wagon" maxlength="50" /></td></tr>
     621<tr><th>Steering:</th><td><select name="steering">
     622<option value="left">Left steering wheel</option>
     623<option value="right" selected="selected">Right steering wheel</option>
     624</select></td></tr>
     625<tr><th>Fuel:</th><td><select name="fuel">
     626<option value="gas" selected="selected">Gasoline</option>
     627<option value="diesel">Diesel</option>
     628<option value="other">Other</option>
     629</select></td></tr>
     630<tr><th>Transmission:</th><td><select name="transmission">
     631<option value="">---------</option>
     632<option value="at" selected="selected">Automatic</option>
     633<option value="mt">Manual</option>
     634<option value="cvt">CVT</option>
     635</select><br />Leave empty if not applicable.</td></tr>
    563636"""}
Back to Top