Skip to content

Instantly share code, notes, and snippets.

@sivy
Created July 21, 2011 00:56
Show Gist options
  • Select an option

  • Save sivy/1096283 to your computer and use it in GitHub Desktop.

Select an option

Save sivy/1096283 to your computer and use it in GitHub Desktop.
CommaStringMultipleChoiceField - store multiple values as a comma-delimited string in Django
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