From 7cd39331440750345720ec816ffdab62c6a87383 Mon Sep 17 00:00:00 2001 From: Kevin Massey Date: Wed, 6 Jan 2016 15:04:51 -0500 Subject: [PATCH] Fixes #3804, avoiding update of Meta.extra_kwargs Added unit test to cover. --- rest_framework/serializers.py | 2 +- tests/test_model_serializer.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 99d36a8a5..c69a20ff1 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -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: diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index af8ce66dd..f3243e33a 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -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)