mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-03 12:00:12 +03:00
Fix read_only + default validation
This commit is contained in:
parent
0327be56c2
commit
2227bc47f8
|
@ -441,6 +441,30 @@ class Serializer(BaseSerializer):
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def _read_only_defaults(self):
|
||||||
|
fields = [
|
||||||
|
field for field in self.fields.values()
|
||||||
|
if (field.read_only) and (field.default != empty) and (field.source != '*') and ('.' not in field.source)
|
||||||
|
]
|
||||||
|
|
||||||
|
defaults = OrderedDict()
|
||||||
|
for field in fields:
|
||||||
|
try:
|
||||||
|
default = field.get_default()
|
||||||
|
except SkipField:
|
||||||
|
continue
|
||||||
|
defaults[field.field_name] = default
|
||||||
|
|
||||||
|
return defaults
|
||||||
|
|
||||||
|
def run_validators(self, value):
|
||||||
|
"""
|
||||||
|
Add read_only fields with defaults to value before running validators.
|
||||||
|
"""
|
||||||
|
to_validate = self._read_only_defaults()
|
||||||
|
to_validate.update(value)
|
||||||
|
super(Serializer, self).run_validators(to_validate)
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
"""
|
"""
|
||||||
Dict of native values <- Dict of primitive datatypes.
|
Dict of native values <- Dict of primitive datatypes.
|
||||||
|
@ -1477,6 +1501,12 @@ class ModelSerializer(Serializer):
|
||||||
if (field.source != '*') and ('.' not in field.source)
|
if (field.source != '*') and ('.' not in field.source)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Special Case: Add read_only fields with defaults.
|
||||||
|
field_names |= {
|
||||||
|
field.source for field in self.fields.values()
|
||||||
|
if (field.read_only) and (field.default != empty) and (field.source != '*') and ('.' not in field.source)
|
||||||
|
}
|
||||||
|
|
||||||
# Note that we make sure to check `unique_together` both on the
|
# Note that we make sure to check `unique_together` both on the
|
||||||
# base model class, but also on any parent classes.
|
# base model class, but also on any parent classes.
|
||||||
validators = []
|
validators = []
|
||||||
|
|
Loading…
Reference in New Issue
Block a user