Merge pull request #5138 from blueyed/fix-star-source-with-HyperlinkedModelSerializer

Fix source="*" with HyperlinkedModelSerializer
This commit is contained in:
Tom Christie 2017-05-16 11:38:22 +01:00 committed by GitHub
commit a8e527a462
2 changed files with 32 additions and 1 deletions

View File

@ -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(

View File

@ -524,6 +524,37 @@ class TestRelationalFieldMappings(TestCase):
""")
self.assertEqual(unicode_repr(TestSerializer()), expected)
def test_nested_hyperlinked_relations_starred_source(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: