Django休息框架嵌套序列化程序与自引用对象 [英] Django rest framework nested serializer with self referential objects

查看:103
本文介绍了Django休息框架嵌套序列化程序与自引用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过在其他地方发布的一些解决方案,但没有运气。看起来它在DRF中不是原生支持的。有没有人有关于如何实现这一点的建议?



我有一个报告模型和一个部分模型。一个部分定义如下:

  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 = 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:第2节,
报告:1,
:1,
section:null,
content:< p> test< / p>
},
{
id:3,
title:第1节,
报告:1,
:1,
section:{
id:1,
title:Section 1,
order:1,
content:< p> test< / p>,
report:1,
section:null
},
content < p为H.试验< / p>中
}
]
}


解决方案

您定义子部分的方式不会将其链接到您的部分字段,就像错误所示。你是否尝试过这样定义你的序列号:

  class SectionSerializer(serializers.ModelSerializer):
class Meta:
model = Section

因为Section有一个FK到section,所以应该像



为确保此序列化程序返回的JSON结果包含嵌套的JSON对象,而不是只有FK,您可以使用两条路由: p>

1), depth =

  class SectionSerializer(serializers.ModelSerializer):
class Meta:
model = Section
depth = 2

这将跟随FKs,构建JSON对象,直到您指定的深度。



2)定义一个SubSerializer来处理JSON对象创建:

  class SubsectionSerializer(serializers.ModelSerializer):
class Meta:
model = Section

class SectionSerializer serializer.ModelSerializer):
section = serializers.SubsectionSerializer()
class Meta:
model = Section
fields =('id','title','report','订单','部分','内容')

---------- --------------编辑---------------------------



为了清楚起见,重命名模型的相关位可能是有意义的:

  class Section (models.Model):
title = models.CharField(max_length = 255)
report = models.ForeignKey(Report)
order = models.PositiveIntegerField()
parent_section = models .ForeignKey('self',related_name ='child_sections',blank = True,null = True)
content = models.TextField(blank = True)

使用新名称s,你应该可以使用下列序列化器:

  class SectionSerializer(serializers.ModelSerializer):
child_sections = serializers.SubsectionSerializer(many = True)
class Meta:
model = Section
fields =('id','title','report','order','child_sections'内容')


I've tried a few solutions posted elsewhere for this problem but with no luck. It seems like it is not natively supported in DRF. Does anyone have suggestions on how to accomplish this?

I have a reports model and a section model. A section is defined as follows:

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)

I want to have it display data like so under reports:

[
    {
        "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>"
            }
        ]
    }
]

My current (attempted) implementation looks like this:

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')

but the output looks like this:

{
    "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>"
        }
    ]
}

解决方案

The way you are defining subsection doesn't link it to your section field, just like the error suggests. Have you tried defining your serializer simply like this:

class SectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Section

Because Section has a FK to section, it should be returned as you would expect from the serializer.

To ensure that the JSON results returned by this serializer contain nested JSON objects, instead of only FKs, there are two routes you can take:

1), depth=

class SectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Section
        depth=2

This will follow FKs down, building JSON objects as it goes to the depth that you specify.

2) Define a SubSerializer to handle the JSON object creation:

class SubsectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Section

class SectionSerializer(serializers.ModelSerializer):
    section = serializers.SubsectionSerializer()
    class Meta:
        model = Section
        fields = ('id', 'title', 'report', 'order', 'section', 'content')

------------------------EDIT---------------------------

For clarity, it might make sense to rename the section related bits of your model:

class Section(models.Model):
    title = models.CharField(max_length=255)
    report = models.ForeignKey(Report)
    order = models.PositiveIntegerField()
    parent_section = models.ForeignKey('self', related_name='child_sections', blank=True, null=True)
    content = models.TextField(blank=True)

With the new names, you should be able to use the following serializer:

class SectionSerializer(serializers.ModelSerializer):
    child_sections = serializers.SubsectionSerializer(many=True)
    class Meta:
        model = Section
        fields = ('id', 'title', 'report', 'order', 'child_sections', 'content')

这篇关于Django休息框架嵌套序列化程序与自引用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆