如何在Django Python中连接3表

2024-06-06 09:45:27 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的第一节课

class dot_bay(models.Model):
    ma_dot_bay = models.CharField(primary_key=True,max_length=255, default=uuid.uuid4, editable=False)
    ten_dot_bay = models.CharField(max_length=255, default="")
    ngay_bay = models.DateTimeField()```

这是我的第二节课

class video(models.Model):
    ma_video = models.CharField(primary_key=True, max_length=255, default=uuid.uuid4, editable=False)
    ma_dot_bay = models.ForeignKey(dot_bay, on_delete=models.CASCADE, related_name='dot_bay_video')
    video_path = models.TextField()
    detected_path = models.TextField()
    ngay_upload = models.TextField()

还有我的第三节课

class hinh_anh(models.Model):
    ma_hinh_anh = models.CharField(primary_key=True, max_length=255, default=uuid.uuid4, editable=False)
    ma_video = models.ForeignKey(video, on_delete=models.CASCADE, related_name='video_hinh_anh')
    image_base64 = models.TextField()

我在我的项目中的序列化程序中尝试了这一点,以显示2个连接表dot_bay和视频的结果 那样

class DotBayModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = dot_bay
        fields = ("ma_dot_bay", "ten_dot_bay", "ngay_bay", "dot_bay_video")
        depth = 1

得到这样的结果

[
    {
        "ma_dot_bay": "db0001",
        "ten_dot_bay": "Đợt bay",
        "ngay_bay": "2021-05-14T15:30:27Z",
        "dot_bay_video": [
            {
                "ma_video": "vd0001",
                "video_path": "1",
                "detected_path": "1",
                "ngay_upload": "1",
                "ma_dot_bay": "db0001"
            },
            {
                "ma_video": "vd0002",
                "video_path": "1",
                "detected_path": "1",
                "ngay_upload": "1",
                "ma_dot_bay": "db0001"
            }
        ]
    },
    {
        "ma_dot_bay": "db0002",
        "ten_dot_bay": "Đợt bay",
        "ngay_bay": "2021-05-14T15:31:07Z",
        "dot_bay_video": [
            {
                "ma_video": "vd0003",
                "video_path": "1",
                "detected_path": "1",
                "ngay_upload": "1",
                "ma_dot_bay": "db0002"
            },
            {
                "ma_video": "vd0004",
                "video_path": "11",
                "detected_path": "1",
                "ngay_upload": "1",
                "ma_dot_bay": "db0002"
            }
        ]
    }
]

这正是我所期望的

但现在我想加入3个表,这样显示

[
    {
        "ma_dot_bay": "db0002",
        "ten_dot_bay": "Đợt bay",
        "ngay_bay": "2021-05-14T15:31:07Z",
        "dot_bay_video": [
            {
                "ma_video": "vd0003",
                "video_path": "1",
                "detected_path": "1",
                "ngay_upload": "1",
                "ma_dot_bay": "db0002",
                "video_hinh_anh": [
                    {
                        "ma_hinh_anh": "....."
                    },
                    {
                        "ma_hinh_anh": "....."
                    }
                ]
            },
            {
                "ma_video": "vd0004",
                "video_path": "11",
                "detected_path": "1",
                "ngay_upload": "1",
                "ma_dot_bay": "db0002",
                "video_hinh_anh": [
                    {
                        "ma_hinh_anh": "....."
                    },
                    {
                        "ma_hinh_anh": "....."
                    }
                ]
            }
        ]
    }
]

我尝试了一些方法,但不起作用:(((( 我该怎么做


Tags: pathmodelsvideodotclassuploadcharfieldbay
1条回答
网友
1楼 · 发布于 2024-06-06 09:45:27

这里的想法是使用嵌套序列化程序。在本例中,我们将使用这些嵌套序列化程序来详细说明外键对象的呈现/结构方式

试试这样:

class NestedHinhAnhModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = hinh_anh
        # Didnt put the `ma_video` field since we're using this serializer inside a video model
        fields = ("ma_hinh_anh ", "image_base_64") 

class NestedVideoModelSerializer(serializers.ModelSerializer):
    # We override the `video_hinh_anh` field with a custom serializer
    video_hinh_anh = NestedHinhAnhModelSerializer(many=True)

    class Meta:
        model = video
        # I didnt put the `ma_dot_bay` fk field since we'll be using this serializer inside the dot_bay model
        fields = ("ma_video ", "video_path ", "detected_path ", "ngay_upload", "video_hinh_anh")

class NestedVideoModelSerializer(serializers.ModelSerializer):
    # We override the `dot_bay_video` with a custom serializer
    dot_bay_video = VideoModelSerializer(many=True)    

    class Meta:
        model = dot_bay
        fields = ("ma_dot_bay", "ten_dot_bay", "ngay_bay", "dot_bay_video")

相关问题 更多 >