Raise error when source= use on a child.

Closes #3292
This commit is contained in:
José Padilla 2015-08-19 11:38:31 -04:00 committed by José Padilla
parent dd850df1d8
commit 8af366a732
2 changed files with 30 additions and 0 deletions

View File

@ -1381,7 +1381,13 @@ class ListField(Field):
def __init__(self, *args, **kwargs):
self.child = kwargs.pop('child', copy.deepcopy(self.child))
self.allow_empty = kwargs.pop('allow_empty', True)
assert not inspect.isclass(self.child), '`child` has not been instantiated.'
assert self.child.source is None, (
"The `source` argument is not meaningful when applied to a `child=` field. "
"Remove `source=` from the field declaration."
)
super(ListField, self).__init__(*args, **kwargs)
self.child.bind(field_name='', parent=self)
@ -1424,7 +1430,13 @@ class DictField(Field):
def __init__(self, *args, **kwargs):
self.child = kwargs.pop('child', copy.deepcopy(self.child))
assert not inspect.isclass(self.child), '`child` has not been instantiated.'
assert self.child.source is None, (
"The `source` argument is not meaningful when applied to a `child=` field. "
"Remove `source=` from the field declaration."
)
super(DictField, self).__init__(*args, **kwargs)
self.child.bind(field_name='', parent=self)

View File

@ -1416,6 +1416,15 @@ class TestListField(FieldValues):
]
field = serializers.ListField(child=serializers.IntegerField())
def test_no_source_on_child(self):
with pytest.raises(AssertionError) as exc_info:
serializers.ListField(child=serializers.IntegerField(source='other'))
assert str(exc_info.value) == (
"The `source` argument is not meaningful when applied to a `child=` field. "
"Remove `source=` from the field declaration."
)
class TestEmptyListField(FieldValues):
"""
@ -1461,6 +1470,15 @@ class TestDictField(FieldValues):
]
field = serializers.DictField(child=serializers.CharField())
def test_no_source_on_child(self):
with pytest.raises(AssertionError) as exc_info:
serializers.DictField(child=serializers.CharField(source='other'))
assert str(exc_info.value) == (
"The `source` argument is not meaningful when applied to a `child=` field. "
"Remove `source=` from the field declaration."
)
class TestUnvalidatedDictField(FieldValues):
"""