Ticket #3652: 4664-choicefield.patch
File 4664-choicefield.patch, 3.7 KB (added by , 18 years ago) |
---|
-
django/db/models/fields/__init__.py
425 425 def formfield(self, **kwargs): 426 426 defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 427 427 defaults.update(kwargs) 428 return forms.CharField(**defaults) 428 if not self.choices: 429 return forms.CharField(**defaults) 430 else: 431 defaults.update(choices=self.choices) 432 return forms.ChoiceField(**defaults) 429 433 434 430 435 # TODO: Maybe move this into contrib, because it's specialized. 431 436 class CommaSeparatedIntegerField(CharField): 432 437 def get_manipulator_field_objs(self): … … 724 729 def formfield(self, **kwargs): 725 730 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 726 731 defaults.update(kwargs) 727 return forms.IntegerField(**defaults) 732 if not self.choices: 733 return forms.IntegerField(**defaults) 734 else: 735 defaults.update(choices=self.choices) 736 return forms.ChoiceField(**defaults) 728 737 729 738 class IPAddressField(Field): 730 739 def __init__(self, *args, **kwargs): -
django/newforms/fields.py
332 332 class ChoiceField(Field): 333 333 def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None, help_text=None): 334 334 super(ChoiceField, self).__init__(required, widget, label, initial, help_text) 335 self.choices = choices335 self.choices = not required and [[u'', u'---------']] + list(choices) or choices 336 336 337 337 def _get_choices(self): 338 338 return self._choices … … 354 354 value = u'' 355 355 value = smart_unicode(value) 356 356 if value == u'': 357 return value357 return None 358 358 valid_values = set([str(k) for k, v in self.choices]) 359 359 if value not in valid_values: 360 360 raise ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.')) -
tests/modeltests/model_forms/models.py
61 61 def __str__(self): 62 62 return self.phone 63 63 64 class Todo(models.Model): 65 what = models.CharField(maxlength=100) 66 importance = models.IntegerField(choices=((1, 'Extremely important'), (2,'Floss cat first')), blank=True, null=True) 67 64 68 __test__ = {'API_TESTS': """ 65 69 >>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField 66 70 >>> import datetime … … 461 465 True 462 466 >>> f.clean_data 463 467 {'phone': u'312-555-1212', 'description': u'Assistance'} 468 469 # ChoiceField ############################################################ 470 471 >>> ChoiceForm = form_for_model(Todo) 472 >>> f = ChoiceForm() 473 >>> print f.as_ul() 474 <li><label for="id_what">What:</label> <input id="id_what" type="text" name="what" maxlength="100" /></li> 475 <li><label for="id_importance">Importance:</label> <select name="importance" id="id_importance"> 476 <option value="" selected="selected">---------</option> 477 <option value="1">Extremely important</option> 478 <option value="2">Floss cat first</option> 479 </select></li> 480 >>> f = ChoiceForm({'what': 'floss cat', 'importance': ''}) 481 >>> f.is_valid() 482 True 483 >>> f.save() 484 <Todo: Todo object> 464 485 """}