Search code examples
pythondjangodjango-modelsdjango-rest-frameworkdjango-serializer

how join 3 table in Django Python


This is my first class

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()```

This is my second class

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()

And my third class

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()

I try this in my Serializer in my project to display result of 2 join table dot_bay and video like that

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

And get the result like that

[
    {
        "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"
            }
        ]
    }
]

that's what I expected

But now I want to join 3 table, display like that,

[
    {
        "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": "....."
                    }
                ]
            }
        ]
    }
]

I try some method but not working :(((( How can I do that ??


Solution

  • The idea here is to use nested serializers. In our case, we'll use those nested serializers to detail how we want foreign key objects to be rendered/structure.

    Try something like that:

    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")