Merge branch 'master' of https://github.com/tomchristie/django-rest-framework into feature/filepathfield

This commit is contained in:
Aider Ibragimov 2015-03-03 14:34:18 +03:00
commit 5f8338e276
2 changed files with 21 additions and 0 deletions

View File

@ -116,6 +116,8 @@ class CreateOnlyDefault:
def set_context(self, serializer_field):
self.is_update = serializer_field.parent.instance is not None
if callable(self.default) and hasattr(self.default, 'set_context'):
self.default.set_context(serializer_field)
def __call__(self):
if self.is_update:

View File

@ -319,6 +319,25 @@ class TestCreateOnlyDefault:
'text': 'example',
}
def test_create_only_default_callable_sets_context(self):
"""
CreateOnlyDefault instances with a callable default should set_context
on the callable if possible
"""
class TestCallableDefault:
def set_context(self, serializer_field):
self.field = serializer_field
def __call__(self):
return "success" if hasattr(self, 'field') else "failure"
class TestSerializer(serializers.Serializer):
context_set = serializers.CharField(default=serializers.CreateOnlyDefault(TestCallableDefault()))
serializer = TestSerializer(data={})
assert serializer.is_valid()
assert serializer.validated_data['context_set'] == 'success'
# Tests for field input and output values.
# ----------------------------------------