Created
January 4, 2018 05:04
-
-
Save blackrobot/d3ae47a02ea6fcdb8eb7345155c3f58a to your computer and use it in GitHub Desktop.
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
def to_internal_value(self, data): | |
""" | |
Dict of native values <- Dict of primitive datatypes. | |
""" | |
if not isinstance(data, Mapping): | |
message = self.error_messages['invalid'].format( | |
datatype=type(data).__name__ | |
) | |
raise ValidationError({ | |
api_settings.NON_FIELD_ERRORS_KEY: [message] | |
}, code='invalid') | |
ret = OrderedDict() | |
errors = OrderedDict() | |
fields = self._writable_fields | |
for field in fields: | |
validate_method = getattr(self, 'validate_' + field.field_name, None) | |
primitive_value = field.get_value(data) | |
try: | |
validated_value = field.run_validation(primitive_value) | |
if validate_method is not None: | |
validated_value = validate_method(validated_value) | |
except ValidationError as exc: | |
errors[field.field_name] = exc.detail | |
except DjangoValidationError as exc: | |
errors[field.field_name] = get_error_detail(exc) | |
except SkipField: | |
pass | |
else: | |
set_value(ret, field.source_attrs, validated_value) | |
if errors: | |
raise ValidationError(errors) | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment