Created
July 21, 2011 00:56
-
-
Save sivy/1096283 to your computer and use it in GitHub Desktop.
CommaStringMultipleChoiceField - store multiple values as a comma-delimited string in Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CommaStringMultipleChoiceField(MultipleChoiceField): | |
| def to_python(self, value): | |
| if isinstance(value,str): | |
| return [val.rstrip().lstrip() for val in value.split(',')] | |
| else: | |
| return [] | |
| def prepare_value(self, value): | |
| return self.to_python(value) | |
| def clean(self, value): | |
| self.validate(value) | |
| if isinstance(value, (list, tuple)): | |
| return ",".join([val.rstrip().lstrip() for val in value]) | |
| else: | |
| return '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment