Created
May 28, 2020 13:36
-
-
Save youtux/43d9983d721abe6d454c8ad4ed599088 to your computer and use it in GitHub Desktop.
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
| """Quick and dirty ScalarListField for django""" | |
| from django import forms | |
| import six | |
| class ScalarListField(forms.CharField): | |
| widget = forms.Textarea | |
| def __init__(self, separator="\n", **kwargs): | |
| self.separator = separator | |
| super(ListField, self).__init__(**kwargs) | |
| def prepare_value(self, value): | |
| # TODO: Maybe call super() | |
| if not value: | |
| return '' | |
| if not isinstance(value, six.string_types): | |
| return self.separator.join(value) | |
| return value | |
| def to_python(self, value): | |
| # TODO: Maybe call super | |
| if not value: | |
| return [] | |
| return [ | |
| v.strip() | |
| for v in value.split(self.separator) | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment