diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index ba8d475f8..40d768975 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -368,6 +368,7 @@ class ModelSerializer(Serializer): models.TimeField: TimeField, models.URLField: URLField, } + nested_class = None # We fill this in at the end of this module. _options_class = ModelSerializerOptions @@ -454,7 +455,7 @@ class ModelSerializer(Serializer): Note that model_field will be `None` for reverse relationships. """ - class NestedModelSerializer(ModelSerializer): + class NestedModelSerializer(self.nested_class): class Meta: model = related_model depth = self.opts.depth - 1 @@ -694,3 +695,7 @@ class HyperlinkedModelSerializer(ModelSerializer): 'app_label': model._meta.app_label, 'model_name': model._meta.object_name.lower() } + + +ModelSerializer.nested_class = ModelSerializer +HyperlinkedModelSerializer.nested_class = HyperlinkedModelSerializer diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py index a32345b31..d946172e4 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_field_mappings.py @@ -150,13 +150,13 @@ class TestRelationalFieldMappings(TestCase): TestSerializer(): url = HyperlinkedIdentityField(view_name='relationalmodel-detail') foreign_key = NestedModelSerializer(read_only=True): - id = IntegerField(label='ID', read_only=True) + url = HyperlinkedIdentityField(view_name='foreignkeytargetmodel-detail') name = CharField(max_length=100) one_to_one = NestedModelSerializer(read_only=True): - id = IntegerField(label='ID', read_only=True) + url = HyperlinkedIdentityField(view_name='onetoonetargetmodel-detail') name = CharField(max_length=100) many_to_many = NestedModelSerializer(many=True, read_only=True): - id = IntegerField(label='ID', read_only=True) + url = HyperlinkedIdentityField(view_name='manytomanytargetmodel-detail') name = CharField(max_length=100) """) self.assertEqual(repr(TestSerializer()), expected)