mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
RelatedField default value handling fixed
This commit is contained in:
parent
6e92e415aa
commit
1addd09e2b
|
@ -301,6 +301,11 @@ class WritableField(Field):
|
|||
result.validators = self.validators[:]
|
||||
return result
|
||||
|
||||
def get_default_value(self):
|
||||
if is_simple_callable(self.default):
|
||||
return self.default()
|
||||
return self.default
|
||||
|
||||
def validate(self, value):
|
||||
if value in validators.EMPTY_VALUES and self.required:
|
||||
raise ValidationError(self.error_messages['required'])
|
||||
|
@ -349,10 +354,7 @@ class WritableField(Field):
|
|||
except KeyError:
|
||||
if self.default is not None and not self.partial:
|
||||
# Note: partial updates shouldn't set defaults
|
||||
if is_simple_callable(self.default):
|
||||
native = self.default()
|
||||
else:
|
||||
native = self.default
|
||||
native = self.get_default_value()
|
||||
else:
|
||||
if self.required:
|
||||
raise ValidationError(self.error_messages['required'])
|
||||
|
|
|
@ -118,6 +118,13 @@ class RelatedField(WritableField):
|
|||
|
||||
choices = property(_get_choices, _set_choices)
|
||||
|
||||
### Default value handling
|
||||
|
||||
def get_default_value(self):
|
||||
default = super(RelatedField, self).get_default_value()
|
||||
return default or \
|
||||
[] if self.many else None
|
||||
|
||||
### Regular serializer stuff...
|
||||
|
||||
def field_to_native(self, obj, field_name):
|
||||
|
@ -166,7 +173,7 @@ class RelatedField(WritableField):
|
|||
except KeyError:
|
||||
if self.partial:
|
||||
return
|
||||
value = [] if self.many else None
|
||||
value = self.get_default_value()
|
||||
|
||||
if value in (None, '') and self.required:
|
||||
raise ValidationError(self.error_messages['required'])
|
||||
|
|
Loading…
Reference in New Issue
Block a user