mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-02 20:54:42 +03:00
Fixed the validation for optional fields that have a value.
This commit is contained in:
parent
caf4d36cb3
commit
51e6982397
|
@ -881,7 +881,7 @@ class ModelSerializer(Serializer):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return ModelField(model_field=model_field, **kwargs)
|
return ModelField(model_field=model_field, **kwargs)
|
||||||
|
|
||||||
def get_validation_exclusions(self):
|
def get_validation_exclusions(self, instance=None):
|
||||||
"""
|
"""
|
||||||
Return a list of field names to exclude from model validation.
|
Return a list of field names to exclude from model validation.
|
||||||
"""
|
"""
|
||||||
|
@ -893,7 +893,7 @@ class ModelSerializer(Serializer):
|
||||||
field_name = field.source or field_name
|
field_name = field.source or field_name
|
||||||
if field_name in exclusions \
|
if field_name in exclusions \
|
||||||
and not field.read_only \
|
and not field.read_only \
|
||||||
and field.required \
|
and (field.required or hasattr(instance, field_name)) \
|
||||||
and not isinstance(field, Serializer):
|
and not isinstance(field, Serializer):
|
||||||
exclusions.remove(field_name)
|
exclusions.remove(field_name)
|
||||||
return exclusions
|
return exclusions
|
||||||
|
@ -908,7 +908,7 @@ class ModelSerializer(Serializer):
|
||||||
the full_clean validation checking.
|
the full_clean validation checking.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
instance.full_clean(exclude=self.get_validation_exclusions())
|
instance.full_clean(exclude=self.get_validation_exclusions(instance))
|
||||||
except ValidationError as err:
|
except ValidationError as err:
|
||||||
self._errors = err.message_dict
|
self._errors = err.message_dict
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user