From 2b199f7588bd72ebdb309313b723d40cbb9b63c8 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 16 May 2017 12:02:23 +0200 Subject: [PATCH 1/3] Add failing test --- tests/test_model_serializer.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 08fc3b42d..707e21eac 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -524,6 +524,38 @@ class TestRelationalFieldMappings(TestCase): """) self.assertEqual(unicode_repr(TestSerializer()), expected) + def test_nested_hyperlinked_relations_star(self): + class TestSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = RelationalModel + depth = 1 + fields = '__all__' + + extra_kwargs = { + 'url': { + 'source': '*', + }} + + expected = dedent(""" + TestSerializer(): + url = HyperlinkedIdentityField(source='*', 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 09f62e11a078dad1e1549c2a819bc4e3274238d8 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 16 May 2017 12:07:04 +0200 Subject: [PATCH 2/3] Possible fix --- rest_framework/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 5bd9b6473..e27610178 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -1010,7 +1010,7 @@ 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', '*') != '*' or field_name # Determine the serializer field class and keyword arguments. field_class, field_kwargs = self.build_field( From 17bf312408b15287267a92596098a672d172a93f Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 16 May 2017 12:10:43 +0200 Subject: [PATCH 3/3] fixup! Add failing test [ci skip] --- tests/test_model_serializer.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 707e21eac..4aa0fa35e 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -524,7 +524,7 @@ class TestRelationalFieldMappings(TestCase): """) self.assertEqual(unicode_repr(TestSerializer()), expected) - def test_nested_hyperlinked_relations_star(self): + def test_nested_hyperlinked_relations_starred_source(self): class TestSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = RelationalModel @@ -553,7 +553,6 @@ class TestRelationalFieldMappings(TestCase): name = CharField(max_length=100) """) self.maxDiff = None - self.assertEqual(unicode_repr(TestSerializer()), expected) def test_nested_unique_together_relations(self):