Django.rest_framework:如何将一对多序列化? [英] Django.rest_framework: How to serialize one to many to many?

查看:563
本文介绍了Django.rest_framework:如何将一对多序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Django进行序列化时遇到了一些麻烦。
我有三种模式,比如学校,房间和书桌(例如,假名)。
每个学校都有多个房间,每个房间都有多个桌子。

I have some troubles serializing with django. I have three models, let's say a School, a Room and a Desk (dummy name for example). Each School have multiple Room, and each Room have multiple Desk.

班级及其关系如下:

class School(models.Model):
    name = models.CharField()

class Room(models.Model):
    name = models.CharField()
    school_id = models.ForeignKey(School)

class Desk(models.Model):
    row = models.IntegerField()
    col = models.IntegerField()
    room_id = models.ForeignKey(Room)

我希望能够序列化School的列表,每个列表直接包含里面的所有桌子。

I want to be able to serialize a list of School, each directly containing all the desks inside.

我得到的壁橱是通过写序列化文件。py3 serializer:

The closet I got was by writing in my serialize.py three serializer :

class DeskSerializer(serializers.ModelSerializer):
    class Meta:
        field = (row, col,)

class RoomSerializer(serializers.ModelSerializer):

    desks = DeskSerializer(source='desk_set', many=True)
    class Meta:
        field = (desks,)

class SchoolSerializer(serializers.ModelSerializer):

    rooms = RoomSerializer(source='room_set', many=True)
    class Meta:
        field = (name, rooms,)

返回哪个学校列表,包含房间列表,包含桌子列表,当我想要包含列表列表的学校列表时

Which return a list of school containing a list of room containing a list of desk, when I want a list of School containing a list of desk

我应该在School序列化程序中使用哪个来源直接返回办公桌?类似于source = room_set.desk_set?或者也许通过使用transform_函数?

Which source should I use in the School serializer to return directly the desk? Something like source='room_set.desk_set'? Or maybe by using a transform_ function?

PS:代码是从头开始编写的,请忽略可能的语法错误

PS: the code is write from scratch on the post, please ignore the possible syntax errors

推荐答案

如果我对您的理解正确,则希望 SchoolSerializer 返回一个嵌套的2层深度的结构,但跳过中间模型。为此,我将在您的 School 模型中创建一个方法来检索 Desk 实例:

If I'm understanding you correctly, you want the SchoolSerializer to return a nested structure 2 levels deep, but skipping the intermediate model. To do this, I would create a method in your School model to retrieve the Desk instances:

class School(models.Model):
    ...

    def get_desks(self):
        rooms = Room.objects.filter(school=self)
        desks = Desk.objects.filter(room__in=rooms)
        return desks

然后,在您的 SchoolSerializer 中包含一个使用该方法的字段,并根据需要根据需要呈现返回的实例 DeskSerializer

Then, in your SchoolSerializer include a field that uses that method and renders the returned instances as you wish via your DeskSerializer:

class SchoolSerializer(serializers.ModelSerializer):
    ...
    desks = DeskSerializer(
        source='get_desks',
        read_only=True
    )

    class Meta:
        field = (name, desks)

了解其工作原理的关键是将模型方法用作 source 参数的值必须简单地返回实例该序列化器的模型序列化器从那里获取它,呈现对象,但是您在该序列化器中定义了它们(在本例中为 DeskSerializer )。

The key to understanding how this works is that the model method used as the value for the source argument must simply return instances of that serializer's model. The serializer takes it from there, rendering the object(s) however you defined them within that serializer (in this case the DeskSerializer).

希望这会有所帮助。

这篇关于Django.rest_framework:如何将一对多序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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