Ticket #4653: form_for_instance_blank_option-r3.diff

File form_for_instance_blank_option-r3.diff, 6.2 KB (added by Ilya Semenov, 12 years ago)

Reformulated the docs for form_for_instance()

  • django/db/models/fields/__init__.py

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

    3030    (3, 'Live'),
     33STEERING_TYPE = (
     34    ('left', 'Left steering wheel'),
     35    ('right', 'Right steering wheel'),
     38FUEL_TYPE = (
     39    ('gas', 'Gasoline'),
     40    ('diesel', 'Diesel'),
     41    ('other', 'Other'),
     45    ('at', 'Automatic'),
     46    ('mt', 'Manual'),
     47    ('cvt', 'CVT'),
    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
     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.')
    7396__test__ = {'API_TESTS': """
    7497>>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField
    7598>>> import datetime
    593616>>> f.cleaned_data
    594617{'phone': u'312-555-1212', 'description': u'Assistance'}
     619# form_for_* blank choices ####################################################
     621Show the form for a new Car. Note that steering field doesn't include the blank choice,
     622because 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>
     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>
     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>
     644Create a Car, and display the form for modifying it. Note that now the fuel
     645selector doesn't include the blank choice as well, since the field is
     646obligatory 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>
     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>
     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>

    272272    Vinay Sajip <vinay_sajip@yahoo.co.uk>
    273273    David Schein
    274274    scott@staplefish.com
     275    Ilya Semenov <semenov@inetss.com>
    275276    serbaut@gmail.com
    276277    John Shaffer <jshaffer2112@gmail.com>
    277278    Pete Shinners <pete@shinners.org>
  • docs/newforms.txt

    18501850    * If the model field has ``choices`` set, then the form field's ``widget``
    18511851      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.
    18541857Finally, note that you can override the form field used for a given model
    18551858field. See "Overriding the default field types" below.
    21002103this data is not bound to the form. You will need to bind data to the
    21012104form before the form can be saved.
     2106Unlike ``form_for_model()``, a choice field in form created by
     2107``form_for_instance()`` will not include the blank choice if the respective model
     2108field has ``blank=False``. The default choice is drawn from the instance, and
     2109there's no need to force a user to make a selection.
    21032111When you call ``save()`` on a form created by ``form_for_instance()``,
    21042112the database instance will be updated. As in ``form_for_model()``, ``save()``
    21052113will raise ``ValueError`` if the data doesn't validate.
Back to Top