RelatedField default value handling fixed

This commit is contained in:
Anton Shutik 2014-02-27 18:34:36 +03:00
parent 6e92e415aa
commit 1addd09e2b
2 changed files with 14 additions and 5 deletions

View File

@ -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'])

View File

@ -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'])