Django Rest Framework嵌套序列化器 [英] Django Rest Framework Nested Serializers

查看:247
本文介绍了Django Rest Framework嵌套序列化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在使用Django rest框架执行两层嵌套时遇到麻烦。我在这里阅读了具有嵌套关系的DRF文档 http://www.django -rest-framework.org/api-guide/relations/ 并成功完成了第一层,即以JSON显示具有多种颜色的样式。虽然不确定如何链接另一层。任何帮助,将不胜感激。

I am currently having trouble executing two layered nesting with Django rest framework. I have read the DRF docs with nested relationships here http://www.django-rest-framework.org/api-guide/relations/ and have successfully done the first layer which is to show styles with many colors in JSON. Not sure how to chain another layer though. Any help would be appreciated. Thanks in advance!

当前输出如下:

[{
    "name": "AAA123",
    "colors": [
        {
            "name": "White"
        }
    ]
},
{
    "name": "BBB222",
    "colors": [
        {
            "name": "White"
        },
        {
            "name": "Pink"
        },
        {
            "name": "Blue"
        }
    ]
}]

所需的输出应如下所示:

The wanted output should be like this:

[{
    "name": "AAA123",
    "colors": [
        {
            "name": "White",
            "sizes": [{name: "S"}, {name: "M"}]
        }
    ]
},
{
    "name": "BBB222",
    "colors": [
        {
            "name": "White",
            "sizes": [{name: "XS"}, {name: "S"}]
        },
        {
            "name": "Pink"
            "sizes": [{name: "XL"}, {name: "XXL"}]
        },
        {
            "name": "Blue"
            "sizes": [{name: "L"}, {name: "XL"}]
        }
    ]
}]

具体来说,我想证明每种样式都有一套颜色,每种样式-颜色组合都有一套尺寸

Specifically, I would like to show that each style has a set of colors attributed to it, and each style-color combination has a set of sizes attributed to it.

下面是我当前的serializers.py和models.py我使用了中间表,因为我计划在将来增加更多的复杂性(例如在样式颜色中,我要附加彩色图片的位置,在样式颜色中,我要附加尺寸)

Below is my current serializers.py and models.py I've used the intermediate table because I plan to add further complexities in the future (e.g. in style-color I would like to attach location of the colored picture and in style-color-size, I will attach measurements)

serializers.py

serializers.py

class ColorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Color
        fields = ['name']


class SizeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Size
        fields = ['name']


class StyleSerializer(serializers.ModelSerializer):
    colors = ColorSerializer(many=True, read_only=True)

    class Meta:
        model = Style
        fields = ['name', 'colors']

models.py

models.py

class Style(models.Model):
    name = models.CharField(max_length=36, unique=True)
    colors = models.ManyToManyField('Color', through='StyleColor')



class Color(models.Model):
    name = models.CharField(max_length=36, unique=True)



class Size(models.Model):
    name = models.CharField(max_length=12, unique=True)



class StyleColor(models.Model):
    style = models.ForeignKey('Style', on_delete=models.CASCADE)
    color = models.ForeignKey('Color', on_delete=models.CASCADE)
    sizes = models.ManyToManyField('Size', through='StyleColorSize')

    class Meta:
        unique_together = ('style', 'color')



class StyleColorSize(models.Model):
    style_color = models.ForeignKey('StyleColor', on_delete=models.CASCADE)
    size = models.ForeignKey('Size', on_delete=models.CASCADE)

    class Meta:
        unique_together = ('style_color', 'size')


推荐答案

感谢所有尝试回答的人。万一有人遇到和我一样的麻烦,请从此链接的答案中获取启发。 在响应中包括中介(通过模型)在Django Rest Framework中

Thanks for all those who tried to answer. Got inspiration from the answer this link in case anyone gets into the same trouble as I did. Include intermediary (through model) in responses in Django Rest Framework.

class StyleSerializer(serializers.ModelSerializer):
    colors = StyleColorSerializer(source='stylecolor_set',
                                  many=True, read_only=True)

    class Meta:
        model = Style
        fields = ('name', 'colors')

这篇关于Django Rest Framework嵌套序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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