1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | class SwithchedValueTypedChoiceField(MultipleChoiceField):
"""
Return selected data as value instead of value html ttribute.
"""
def to_python(self, value):
if not value:
return []
elif not isinstance(value, (list, tuple)):
raise ValidationError(self.error_messages['invalid_list'])
def switched(value, choices):
"""
Switched value generator
"""
count = 0
max_count = len(value)
while count < max_count:
yield choices[value[count]]
count += 1
value = list(switched(value, dict(self._choices)))
return 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 options
for k2, v2 in v:
if value == smart_unicode(v2):
return True
else:
if value == smart_unicode(v):
return True
return False
|
More like this
- Multiple-Submit-Button Widget for Choice Field by Archatas 4 years, 9 months ago
- The model field subclass returns string generated from the list of choices. by I159 1 year, 6 months ago
- Improved Pickled Object Field by taavi223 3 years, 9 months ago
- "Ukrainian telephone number" model and form fields. by I159 1 year, 7 months ago
- Form rendering using a template instead of builtin HTML by leoh 5 years, 11 months ago
Comments