我已经尝试了一些其他地方发布的解决方案来解决这个问题,但没有运气。似乎 DRF 本身并不支持它。有没有人有关于如何实现这一点的建议?
我有一个 reports
模型和 section
模型。一个部分的定义如下:
class Section(models.Model):
title = models.CharField(max_length=255)
report = models.ForeignKey(Report)
order = models.PositiveIntegerField()
section = models.ForeignKey('self', related_name='section_section', blank=True, null=True)
content = models.TextField(blank=True)
我想让它在报告下显示这样的数据:
[
{
"id": 1,
"title": "test",
"subtitle": "test",
"section_set": [
{
"id": 1,
"title": "test",
"report": 1,
"order": 1,
"section_set": [
{
"id": 1,
"title": "test",
"report": 1,
"order": 1,
"section": null,
"content": "<p>test</p>"
},
{
"id": 2,
"title": "test",
"report": 1,
"order": 1,
"section": 2,
"content": "<p>test</p>"
},
{
"id": 3,
"title": "test",
"report": 1,
"order": 1,
"section": null,
"content": "<p>test</p>"
}
],
"content": "<p>test</p>"
},
{
"id": 2,
"title": "test",
"report": 1,
"order": 1,
"section": 2,
"content": "<p>test</p>"
},
{
"id": 3,
"title": "test",
"report": 1,
"order": 1,
"section": null,
"content": "<p>test</p>"
}
]
}
]
我当前(尝试)的实现如下所示:
class SubsectionSerializer(serializers.ModelSerializer):
class Meta:
model = Section
class SectionSerializer(serializers.ModelSerializer):
section = SubsectionSerializer()
class Meta:
model = Section
fields = ('id', 'title', 'report', 'order', 'section', 'content')
class CountryReportSerializer(serializers.ModelSerializer):
section_set = SectionSerializer(many=True)
class Meta:
model = CountryReport
fields = ('id', 'title', 'subtitle', 'section_set')
class MapsSerializer(serializers.ModelSerializer):
class Meta:
model = Map
fields = ('id', 'country', 'map_image', 'report')
但输出如下所示:
{
"id": 1,
"title": "test",
"subtitle": "test",
"section_set": [
{
"id": 1,
"title": "Section 1",
"report": 1,
"order": 1,
"section": null,
"content": "<p>test</p>"
},
{
"id": 2,
"title": "Section 2",
"report": 1,
"order": 1,
"section": null,
"content": "<p>test</p>"
},
{
"id": 3,
"title": "Subsection 1",
"report": 1,
"order": 1,
"section": {
"id": 1,
"title": "Section 1",
"order": 1,
"content": "<p>test</p>",
"report": 1,
"section": null
},
"content": "<p>test</p>"
}
]
}
最佳答案
得到它与以下解决方案一起工作:
class RecursiveField(serializers.Serializer):
def to_representation(self, value):
serializer = self.parent.parent.__class__(value, context=self.context)
return serializer.data
class SectionSerializer(serializers.ModelSerializer):
children = RecursiveField(many=True)
class Meta:
model = Section
fields = ('id', 'order', 'title', 'content', 'parent', 'children')
class CountryReportSerializer(serializers.ModelSerializer):
section_set = serializers.SerializerMethodField('get_parent_sections')
@staticmethod
def get_parent_sections(self, obj):
parent_sections = Section.objects.get(parent=None, pk=obj.pk)
serializer = SectionSerializer(parent_sections)
return serializer.data
class Meta:
model = CountryReport
fields = ('id', 'title', 'subtitle', 'section_set')
class MapsSerializer(serializers.ModelSerializer):
class Meta:
model = Map
fields = ('id', 'country', 'map_image', 'report')
关于带有自引用对象的 Django 休息框架嵌套序列化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33487419/