Django:将序列化器相互嵌套

时间:2015-11-27 12:12:12

标签: json django django-models django-rest-framework django-serializer

我正在弄清楚序列化程序如何合并或嵌套到彼此中。在这个例子中,我有一个列模型(由Column类组成)和属于列模型的数据(由Data class组成)。我的问题是我不知道如何从另一个序列化程序类调用ModelSerializer并传递参数(结果总是为空)。

如果我的模型对于这种情况是正确的,以及如何创建所需的JSON以便结果重用现有的序列化程序并避免重复任何数据,你能告诉我吗?

注意:在最好的情况下,数据属性应该相互依赖,以便只有那些被定义为列的序列化数据。

models.py

class Column(models.Model):
    data = models.CharField(max_length=200)
    title = models.CharField(max_length=200)

    def __str__(self):
        return self.order

class Data(models.Model):
    doc = models.CharField(max_length=200)
    order = models.CharField(max_length=200)
    nothing = models.CharField(max_length=200)

    def __str__(self):
        return self.order

期望的输出:

{
    "columns": [
        {
            "data": "doc",
            "title": "Doc."
        },
        {
            "data": "order",
            "title": "Order no."
        },
        {
            "data": "nothing",
            "title": "Nothing"
        }
    ],
    "data": [
        {
            "doc": "564251422",
            "nothing": 0.0,
            "order": "56421"
        },
        {
            "doc": "546546545",
            "nothing": 0.0,
            "order": "98745"
        }
    ]
}

但是ModelSerializer的结果是这样的:

[
    {
        "doc": "564251422",
        "order": "56421",
        "nothing": "0.0"
    },
    {
        "doc": "546546545",
        "order": "98745",
        "nothing": "0.0"
    }
]

2 个答案:

答案 0 :(得分:2)

您必须添加包含columnsdata属性的模型,因为它们当前未链接。

您的models.py文件:

class Table(models.Model):
    pass

class Column(models.Model):
    data = models.CharField(max_length=200)
    title = models.CharField(max_length=200)
    table = models.ForeignKey(Table)

    def __str__(self):
        return self.order

class Line(models.Model):
    doc = models.CharField(max_length=200)
    order = models.CharField(max_length=200)
    nothing = models.CharField(max_length=200)
    table = models.ForeignKey(Table)

    def __str__(self):
        return self.order

您的serializer.py文件:

# import your related models and serializers

class ColumnSerializer(serializers.ModelSerializer):

    class Meta:
        model = Column
        fields = [
            'data',
            'title'
        ]

class LineSerializer(serializers.ModelSerializer):

    class Meta:
        model = Line
        fields = [
            'doc',
            'order',
            'nothing'
        ]

class TableSerializer(serializers.ModelSerializer):
    columns = ColumnSerializer(many=True)
    lines = LineSerializer(many=True)

    class Meta:
        model = Table
        fields = [
            'columns',
            'lines'
        ]

现在使用TableSerializer序列化程序序列化和反序列化Table对象。

关于您的模型,Line代替Data可能更合适。并且

阅读Django-Rest-Framework - NestedRelationships以获取更多信息,并了解如何支持嵌套序列化程序字段的写操作。

答案 1 :(得分:0)

首先,您需要修改模型。您可以在data模型中设置ForeignKey Column字段,如:

class Column(models.Model):
    data = models.ForeignKey("Data")
    title = models.CharField(max_length=200)

接下来为Data创建一个新的序列化程序,如:

class DataSerializer(serializers.ModelSerializer):

    class Meta:
        model = Data

现在,您可以在DataSerializer中使用ColumnSerializer获取每列的数据,如:

class ColumnSerializer(serializers.ModelSerializer):
    data = DataSerializer(read_only=True)

    class Meta:
        model = Column

这会产生如下输出:

[
    {
        "title" : "Doc",
        "data" :{
            "doc": "564251422",
            "nothing": 0.0,
            "order": "56421"
        }
     },
     {
        "title" : "Order no.",
        "data" :{
            "doc": "546546545",
            "nothing": 0.0,
            "order": "98745"
        }
    }
]