From d0f5a58fbd73e1a9ccef405e16ac070787e98fd4 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Thu, 29 Nov 2007 16:32:18 +0000 Subject: Fixed #4653 -- Improved the logic to decide when to include (and select 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 --- tests/modeltests/model_forms/models.py | 73 ++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'tests/modeltests/model_forms') diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index e4e230c98d..9b0126ff4f 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -30,6 +30,23 @@ ARTICLE_STATUS = ( (3, 'Live'), ) +STEERING_TYPE = ( + ('left', 'Left steering wheel'), + ('right', 'Right steering wheel'), +) + +FUEL_TYPE = ( + ('gas', 'Gasoline'), + ('diesel', 'Diesel'), + ('other', 'Other'), +) + +TRANSMISSION_TYPE = ( + ('at', 'Automatic'), + ('mt', 'Manual'), + ('cvt', 'CVT'), +) + class Category(models.Model): name = models.CharField(max_length=20) slug = models.SlugField(max_length=20) @@ -70,6 +87,12 @@ class PhoneNumber(models.Model): def __unicode__(self): return self.phone +class Car(models.Model): + name = models.CharField(max_length=50) + steering = models.CharField(max_length=5, choices=STEERING_TYPE, default='left') + fuel = models.CharField(max_length=10, choices=FUEL_TYPE) + transmission = models.CharField(max_length=3, choices=TRANSMISSION_TYPE, blank=True, help_text='Leave empty if not applicable.') + __test__ = {'API_TESTS': """ >>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField >>> import datetime @@ -592,4 +615,54 @@ ValidationError: [u'Select a valid choice. 4 is not one of the available choices True >>> f.cleaned_data {'phone': u'312-555-1212', 'description': u'Assistance'} + +# form_for_* blank choices #################################################### + +Show the form for a new Car. Note that steering field doesn't include the blank choice, +because the field is obligatory and has an explicit default. +>>> CarForm = form_for_model(Car) +>>> f = CarForm(auto_id=False) +>>> print f +Name: +Steering: +Fuel: +Transmission:
Leave empty if not applicable. + +Create a Car, and display the form for modifying it. Note that now the fuel +selector doesn't include the blank choice as well, since the field is +obligatory and can not be changed to be blank. +>>> honda = Car(name='Honda Accord Wagon', steering='right', fuel='gas', transmission='at') +>>> honda.save() +>>> HondaForm = form_for_instance(honda) +>>> f = HondaForm(auto_id=False) +>>> print f +Name: +Steering: +Fuel: +Transmission:
Leave empty if not applicable. """} -- cgit v1.3