Fixes #3804, avoiding update of Meta.extra_kwargs

Added unit test to cover.
This commit is contained in:
Kevin Massey 2016-01-06 15:04:51 -05:00
parent c46ed66d0a
commit 7cd3933144
2 changed files with 32 additions and 1 deletions

View File

@ -1212,7 +1212,7 @@ class ModelSerializer(Serializer):
Return a dictionary mapping field names to a dictionary of
additional keyword arguments.
"""
extra_kwargs = getattr(self.Meta, 'extra_kwargs', {})
extra_kwargs = copy.deepcopy(getattr(self.Meta, 'extra_kwargs', {}))
read_only_fields = getattr(self.Meta, 'read_only_fields', None)
if read_only_fields is not None:

View File

@ -909,3 +909,34 @@ class TestDecimalFieldMappings(TestCase):
serializer = TestSerializer()
assert serializer.fields['decimal_field'].max_value == 3
class TestMetaInheritance(TestCase):
def test_extra_kwargs_not_altered(self):
class TestSerializer(serializers.ModelSerializer):
non_model_field = serializers.CharField()
class Meta:
model = OneFieldModel
read_only_fields = ('char_field', 'non_model_field')
fields = read_only_fields
extra_kwargs = {}
class ChildSerializer(TestSerializer):
class Meta(TestSerializer.Meta):
read_only_fields = ()
test_expected = dedent("""
TestSerializer():
char_field = CharField(read_only=True)
non_model_field = CharField()
""")
child_expected = dedent("""
ChildSerializer():
char_field = CharField(max_length=100)
non_model_field = CharField()
""")
self.assertEqual(unicode_repr(ChildSerializer()), child_expected)
self.assertEqual(unicode_repr(TestSerializer()), test_expected)
self.assertEqual(unicode_repr(ChildSerializer()), child_expected)