From 3c1bf6bfd5d69f36c822f92d0bb22a6f338c6431 Mon Sep 17 00:00:00 2001 From: jhg14 Date: Mon, 4 Sep 2017 11:47:53 +0100 Subject: [PATCH 1/3] Add failing test for named attribute Fix test crudely Remove comment --- rest_framework/serializers.py | 4 +++- tests/test_model_serializer.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index a4b51ae9d..b1c34b92a 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -1010,7 +1010,9 @@ class ModelSerializer(Serializer): continue extra_field_kwargs = extra_kwargs.get(field_name, {}) - source = extra_field_kwargs.get('source', '*') != '*' or field_name + source = extra_field_kwargs.get('source', '*') + if source == '*': + source = field_name # Determine the serializer field class and keyword arguments. field_class, field_kwargs = self.build_field( diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index ba3edd389..ce054f695 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -557,6 +557,39 @@ class TestRelationalFieldMappings(TestCase): self.maxDiff = None self.assertEqual(unicode_repr(TestSerializer()), expected) + def test_nested_hyperlinked_relations_named_source(self): + class TestSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = RelationalModel + depth = 1 + fields = '__all__' + + extra_kwargs = { + 'url': { + 'source': 'url' + }} + + expected = dedent(""" + TestSerializer(): + url = HyperlinkedIdentityField(source='url', view_name='relationalmodel-detail') + foreign_key = NestedSerializer(read_only=True): + url = HyperlinkedIdentityField(view_name='foreignkeytargetmodel-detail') + name = CharField(max_length=100) + one_to_one = NestedSerializer(read_only=True): + url = HyperlinkedIdentityField(view_name='onetoonetargetmodel-detail') + name = CharField(max_length=100) + many_to_many = NestedSerializer(many=True, read_only=True): + url = HyperlinkedIdentityField(view_name='manytomanytargetmodel-detail') + name = CharField(max_length=100) + through = NestedSerializer(many=True, read_only=True): + url = HyperlinkedIdentityField(view_name='throughtargetmodel-detail') + name = CharField(max_length=100) + """) + self.maxDiff = None + self.assertEqual(unicode_repr(TestSerializer()), expected) + + + def test_nested_unique_together_relations(self): class TestSerializer(serializers.HyperlinkedModelSerializer): class Meta: From 43458944452fe11132fa1ab5f2bf1934b8fc108f Mon Sep 17 00:00:00 2001 From: jhg14 Date: Mon, 4 Sep 2017 15:41:04 +0100 Subject: [PATCH 2/3] Add simplest possible failing test --- tests/test_model_serializer.py | 55 ++++++++++++++-------------------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index ce054f695..430be71c6 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -557,39 +557,6 @@ class TestRelationalFieldMappings(TestCase): self.maxDiff = None self.assertEqual(unicode_repr(TestSerializer()), expected) - def test_nested_hyperlinked_relations_named_source(self): - class TestSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = RelationalModel - depth = 1 - fields = '__all__' - - extra_kwargs = { - 'url': { - 'source': 'url' - }} - - expected = dedent(""" - TestSerializer(): - url = HyperlinkedIdentityField(source='url', view_name='relationalmodel-detail') - foreign_key = NestedSerializer(read_only=True): - url = HyperlinkedIdentityField(view_name='foreignkeytargetmodel-detail') - name = CharField(max_length=100) - one_to_one = NestedSerializer(read_only=True): - url = HyperlinkedIdentityField(view_name='onetoonetargetmodel-detail') - name = CharField(max_length=100) - many_to_many = NestedSerializer(many=True, read_only=True): - url = HyperlinkedIdentityField(view_name='manytomanytargetmodel-detail') - name = CharField(max_length=100) - through = NestedSerializer(many=True, read_only=True): - url = HyperlinkedIdentityField(view_name='throughtargetmodel-detail') - name = CharField(max_length=100) - """) - self.maxDiff = None - self.assertEqual(unicode_repr(TestSerializer()), expected) - - - def test_nested_unique_together_relations(self): class TestSerializer(serializers.HyperlinkedModelSerializer): class Meta: @@ -1168,3 +1135,25 @@ class Test5004UniqueChoiceField(TestCase): serializer = TestUniqueChoiceSerializer(data={'name': 'choice1'}) assert not serializer.is_valid() assert serializer.errors == {'name': ['unique choice model with this name already exists.']} + +class TestFieldSource(TestCase): + + def test_named_field_source(self): + class TestSerializer(serializers.ModelSerializer): + + class Meta: + model = RegularFieldsModel + fields = ('number_field',) + extra_kwargs = { + 'number_field': { + 'source': 'integer_field' + } + } + + expected = dedent(""" + TestSerializer(): + number_field = IntegerField(source='integer_field') + """) + self.maxDiff = None + self.assertEqual(unicode_repr(TestSerializer()), expected) + From 66b2c6149ed0121b1064fe42615aaa8d75aa5d8b Mon Sep 17 00:00:00 2001 From: jhg14 Date: Mon, 4 Sep 2017 16:17:43 +0100 Subject: [PATCH 3/3] Fix code style --- tests/test_model_serializer.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 430be71c6..3411c44b5 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -1136,13 +1136,13 @@ class Test5004UniqueChoiceField(TestCase): assert not serializer.is_valid() assert serializer.errors == {'name': ['unique choice model with this name already exists.']} + class TestFieldSource(TestCase): - def test_named_field_source(self): class TestSerializer(serializers.ModelSerializer): class Meta: - model = RegularFieldsModel + model = RegularFieldsModel fields = ('number_field',) extra_kwargs = { 'number_field': { @@ -1156,4 +1156,3 @@ class TestFieldSource(TestCase): """) self.maxDiff = None self.assertEqual(unicode_repr(TestSerializer()), expected) -