Django Rest Framework嵌套序列化器

编程入门 行业动态 更新时间:2024-10-27 00:25:25
本文介绍了Django Rest Framework嵌套序列化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前在使用Django rest框架执行两层嵌套时遇到麻烦。我在这里阅读了具有嵌套关系的DRF文档 http://www.django -rest-framework/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 www.django-rest-framework/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嵌套序列化器

本文发布于:2023-10-10 01:16:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1477288.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   序列化   Django   Rest   Framework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!