--To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/ezZh3lD2mRcJ.
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
class ChoiceField(Field):widget = Selectdefault_error_messages = {'invalid_choice': _(u'Select a valid choice. %(value)s is not one of the available choices.'),}def __init__(self, choices=(), required=True, widget=None, label=None,initial=None, help_text=None, *args, **kwargs):super(ChoiceField, self).__init__(required=required, widget=widget, label=label,initial=initial, help_text=help_text, *args, **kwargs)self.choices = choicesdef __deepcopy__(self, memo):result = super(ChoiceField, self).__deepcopy__(memo)result._choices = copy.deepcopy(self._choices, memo)return resultdef _get_choices(self):return self._choicesdef _set_choices(self, value):# Setting choices also sets the choices on the widget.# choices can be any iterable, but we call list() on it because# it will be consumed more than once.self._choices = self.widget.choices = list(value)choices = property(_get_choices, _set_choices)def to_python(self, value):"Returns a Unicode object."if value in validators.EMPTY_VALUES:return u''return smart_unicode(value)def validate(self, value):"""Validates that the input is in self.choices."""super(ChoiceField, self).validate(value)if value and not self.valid_value(value):raise ValidationError(self.error_messages['invalid_choice'] % {'value': value})def valid_value(self, value):"Check to see if the provided value is a valid choice"for k, v in self.choices:if isinstance(v, (list, tuple)):# This is an optgroup, so look inside the group for optionsfor k2, v2 in v:if value == smart_unicode(k2):return Trueelse:if value == smart_unicode(k):return Truereturn False
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/FHF67cmYklQJ.