Fix nested model serializer base class

This commit is contained in:
Tom Christie 2014-09-15 13:55:09 +01:00
parent 0c15b97b33
commit d196608d5a
2 changed files with 9 additions and 4 deletions

View File

@ -368,6 +368,7 @@ class ModelSerializer(Serializer):
models.TimeField: TimeField, models.TimeField: TimeField,
models.URLField: URLField, models.URLField: URLField,
} }
nested_class = None # We fill this in at the end of this module.
_options_class = ModelSerializerOptions _options_class = ModelSerializerOptions
@ -454,7 +455,7 @@ class ModelSerializer(Serializer):
Note that model_field will be `None` for reverse relationships. Note that model_field will be `None` for reverse relationships.
""" """
class NestedModelSerializer(ModelSerializer): class NestedModelSerializer(self.nested_class):
class Meta: class Meta:
model = related_model model = related_model
depth = self.opts.depth - 1 depth = self.opts.depth - 1
@ -694,3 +695,7 @@ class HyperlinkedModelSerializer(ModelSerializer):
'app_label': model._meta.app_label, 'app_label': model._meta.app_label,
'model_name': model._meta.object_name.lower() 'model_name': model._meta.object_name.lower()
} }
ModelSerializer.nested_class = ModelSerializer
HyperlinkedModelSerializer.nested_class = HyperlinkedModelSerializer

View File

@ -150,13 +150,13 @@ class TestRelationalFieldMappings(TestCase):
TestSerializer(): TestSerializer():
url = HyperlinkedIdentityField(view_name='relationalmodel-detail') url = HyperlinkedIdentityField(view_name='relationalmodel-detail')
foreign_key = NestedModelSerializer(read_only=True): foreign_key = NestedModelSerializer(read_only=True):
id = IntegerField(label='ID', read_only=True) url = HyperlinkedIdentityField(view_name='foreignkeytargetmodel-detail')
name = CharField(max_length=100) name = CharField(max_length=100)
one_to_one = NestedModelSerializer(read_only=True): 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) name = CharField(max_length=100)
many_to_many = NestedModelSerializer(many=True, read_only=True): 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) name = CharField(max_length=100)
""") """)
self.assertEqual(repr(TestSerializer()), expected) self.assertEqual(repr(TestSerializer()), expected)