mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
Modify 'to_internal_value' method for handling list
modified the 'get_value' method in fields.py, The value of primitive_value is a list of values to match the field. Validate the primitive_value (list) elements one by one using the run_validation method. The verified value is appended to the validated_list Finally, only when there are two or more elements of validated_list, they are returned as a list.
This commit is contained in:
parent
d4cc5478dd
commit
c66df67c49
|
@ -454,26 +454,29 @@ class Serializer(BaseSerializer):
|
|||
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
|
||||
validated_list = []
|
||||
for inner_data in primitive_value:
|
||||
try:
|
||||
validated_value = field.run_validation(inner_data)
|
||||
validated_list.append(validated_value)
|
||||
if validate_method is not None:
|
||||
validated_list.append(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 len(validated_list) > 1:
|
||||
set_value(ret, field.source_attrs, validated_list)
|
||||
else:
|
||||
set_value(ret, field.source_attrs, validated_list[0])
|
||||
if errors:
|
||||
raise ValidationError(errors)
|
||||
|
||||
return ret
|
||||
|
||||
def to_representation(self, instance):
|
||||
|
|
Loading…
Reference in New Issue
Block a user