- Author:
- I159
- Posted:
- November 16, 2011
- Language:
- Python
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
As it took me a save option name instead of its value. This field are solves this rare problem.
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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.