Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4653 -- Improved the logic to decide when to include (and sele…

…ct as

initial value) the blank choice for a model field with choices. Thanks to
Ilya Semenov for persisting with this.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6733 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d0f5a58fbd73e1a9ccef405e16ac070787e98fd4 1 parent a4ea8d4
Malcolm Tredinnick authored November 29, 2007
1  AUTHORS
@@ -273,6 +273,7 @@ answer newbie questions, and generally made Django that much better:
273 273
     Vinay Sajip <vinay_sajip@yahoo.co.uk>
274 274
     David Schein
275 275
     scott@staplefish.com
  276
+    Ilya Semenov <semenov@inetss.com>
276 277
     serbaut@gmail.com
277 278
     John Shaffer <jshaffer2112@gmail.com>
278 279
     Pete Shinners <pete@shinners.org>
2  django/db/models/fields/__init__.py
@@ -392,7 +392,7 @@ def formfield(self, form_class=forms.CharField, **kwargs):
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)
18  docs/newforms.txt
@@ -1849,7 +1849,11 @@ In addition, each generated form field has attributes set as follows:
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 normally include the blank choice which is
  1853
+      selected by default. If the field is required, this forces the user to
  1854
+      make a selection. The blank choice will not be included if the model
  1855
+      field has ``blank=False`` and an explicit ``default`` value (the
  1856
+      ``default`` value will be initially selected instead).
1853 1857
 
1854 1858
 Finally, note that you can override the form field used for a given model
1855 1859
 field. See "Overriding the default field types" below.
@@ -2095,10 +2099,14 @@ instance instead of a model class::
2095 2099
     # Instantiate the form.
2096 2100
     >>> f = AuthorForm()
2097 2101
 
2098  
-When a form created by ``form_for_instance()`` is created, the initial
2099  
-data values for the form fields are drawn from the instance. However,
2100  
-this data is not bound to the form. You will need to bind data to the
2101  
-form before the form can be saved.
  2102
+When a form created by ``form_for_instance()`` is created, the initial data
  2103
+values for the form fields are drawn from the instance. However, this data is
  2104
+not bound to the form. You will need to bind data to the form before the form
  2105
+can be saved.
  2106
+
  2107
+Unlike ``form_for_model()``, a choice field in form created by
  2108
+``form_for_instance()`` will not include the blank choice if the respective
  2109
+model field has ``blank=False``. The initial choice is drawn from the instance.
2102 2110
 
2103 2111
 When you call ``save()`` on a form created by ``form_for_instance()``,
2104 2112
 the database instance will be updated. As in ``form_for_model()``, ``save()``
73  tests/modeltests/model_forms/models.py
@@ -30,6 +30,23 @@
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,6 +87,12 @@ class PhoneNumber(models.Model):
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,4 +615,54 @@ def __unicode__(self):
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
 """}

0 notes on commit d0f5a58

Please sign in to comment.
Something went wrong with that request. Please try again.