-
-
Save marceloleiva/46c38474f9c1303d5c09d3a2fb8c8970 to your computer and use it in GitHub Desktop.
Comma Separated Values Form Field for Django. There are CommaSeparatedCharField, CommaSeparatedIntegerField.
This file contains 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
from django import forms | |
from django.core import validators | |
from django.core.exceptions import ValidationError | |
class MinLengthValidator(validators.MinLengthValidator): | |
message = 'Ensure this value has at least %(limit_value)d elements (it has %(show_value)d).' | |
class MaxLengthValidator(validators.MaxLengthValidator): | |
message = 'Ensure this value has at most %(limit_value)d elements (it has %(show_value)d).' | |
class CommaSeparatedCharField(forms.Field): | |
def __init__(self, dedup=True, max_length=None, min_length=None, *args, **kwargs): | |
self.dedup, self.max_length, self.min_length = dedup, max_length, min_length | |
super(CommaSeparatedCharField, self).__init__(*args, **kwargs) | |
if min_length is not None: | |
self.validators.append(MinLengthValidator(min_length)) | |
if max_length is not None: | |
self.validators.append(MaxLengthValidator(max_length)) | |
def to_python(self, value): | |
if value in validators.EMPTY_VALUES: | |
return [] | |
value = [item.strip() for item in value.split(',') if item.strip()] | |
if self.dedup: | |
value = list(set(value)) | |
return value | |
def clean(self, value): | |
value = self.to_python(value) | |
self.validate(value) | |
self.run_validators(value) | |
return value | |
class CommaSeparatedIntegerField(forms.Field): | |
default_error_messages = { | |
'invalid': 'Enter comma separated numbers only.', | |
} | |
def __init__(self, dedup=True, max_length=None, min_length=None, *args, **kwargs): | |
self.dedup, self.max_length, self.min_length = dedup, max_length, min_length | |
super(CommaSeparatedIntegerField, self).__init__(*args, **kwargs) | |
if min_length is not None: | |
self.validators.append(MinLengthValidator(min_length)) | |
if max_length is not None: | |
self.validators.append(MaxLengthValidator(max_length)) | |
def to_python(self, value): | |
if value in validators.EMPTY_VALUES: | |
return [] | |
try: | |
value = [int(item.strip()) for item in value.split(',') if item.strip()] | |
if self.dedup: | |
value = list(set(value)) | |
except (ValueError, TypeError): | |
raise ValidationError(self.error_messages['invalid']) | |
return value | |
def clean(self, value): | |
value = self.to_python(value) | |
self.validate(value) | |
self.run_validators(value) | |
return value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment