Created
March 30, 2017 17:41
-
-
Save smmoosavi/349052c371975ae5efe7d3b678f60aa1 to your computer and use it in GitHub Desktop.
Array Form Field
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.contrib.postgres.forms import SimpleArrayField, ValidationError, prefix_validation_error | |
class ArrayField(SimpleArrayField): | |
def prepare_value(self, value): | |
return [self.base_field.prepare_value(v) for v in value] | |
def to_python(self, value): | |
# see django/contrib/postgres/forms/array.py:37 | |
if value: | |
items = value | |
else: | |
items = [] | |
errors = [] | |
values = [] | |
for index, item in enumerate(items): | |
try: | |
values.append(self.base_field.to_python(item)) | |
except ValidationError as error: | |
errors.append(prefix_validation_error( | |
error, | |
prefix=self.error_messages['item_invalid'], | |
code='item_invalid', | |
params={'nth': index}, | |
)) | |
if errors: | |
raise ValidationError(errors) | |
return values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/django/django/blob/c651331b34b7c3841c126959e6e52879bc6f0834/django/contrib/postgres/forms/array.py#L31-L42