mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 19:14:01 +03:00
Fix nested model serializer base class
This commit is contained in:
parent
0c15b97b33
commit
d196608d5a
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user