Ticket #3652: 4664-choicefield.patch

File 4664-choicefield.patch, 3.7 KB (added by Øyvind Saltvik <oyvind.saltvik@…>, 18 years ago)

ChoiceField patch

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

     
    425425    def formfield(self, **kwargs):
    426426        defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
    427427        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)
    429433
     434
    430435# TODO: Maybe move this into contrib, because it's specialized.
    431436class CommaSeparatedIntegerField(CharField):
    432437    def get_manipulator_field_objs(self):
     
    724729    def formfield(self, **kwargs):
    725730        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
    726731        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)
    728737
    729738class IPAddressField(Field):
    730739    def __init__(self, *args, **kwargs):
  • django/newforms/fields.py

     
    332332class ChoiceField(Field):
    333333    def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None, help_text=None):
    334334        super(ChoiceField, self).__init__(required, widget, label, initial, help_text)
    335         self.choices = choices
     335        self.choices = not required and [[u'', u'---------']] + list(choices) or choices
    336336
    337337    def _get_choices(self):
    338338        return self._choices
     
    354354            value = u''
    355355        value = smart_unicode(value)
    356356        if value == u'':
    357             return value
     357            return None
    358358        valid_values = set([str(k) for k, v in self.choices])
    359359        if value not in valid_values:
    360360            raise ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
  • tests/modeltests/model_forms/models.py

     
    6161    def __str__(self):
    6262        return self.phone
    6363
     64class 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
    6468__test__ = {'API_TESTS': """
    6569>>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField
    6670>>> import datetime
     
    461465True
    462466>>> f.clean_data
    463467{'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()
     482True
     483>>> f.save()
     484<Todo: Todo object>
    464485"""}
Back to Top