如何在 Django Python 中加入 3 table
how join 3 table in Django Python
这是我的第一个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()```
这是我的第二个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()
还有我的第三个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()
我在我的项目序列化器中尝试这个来显示 2 join table 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个table,这样显示,
[
{
"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": "....."
}
]
}
]
}
]
我尝试了一些方法但没有用:((((
我该怎么做??
这里的想法是使用嵌套序列化程序。在我们的例子中,我们将使用那些嵌套的序列化程序来详细说明我们希望外键对象如何成为 rendered/structure.
尝试这样的事情:
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")
这是我的第一个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()```
这是我的第二个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()
还有我的第三个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()
我在我的项目序列化器中尝试这个来显示 2 join table 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个table,这样显示,
[
{
"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": "....."
}
]
}
]
}
]
我尝试了一些方法但没有用:(((( 我该怎么做??
这里的想法是使用嵌套序列化程序。在我们的例子中,我们将使用那些嵌套的序列化程序来详细说明我们希望外键对象如何成为 rendered/structure.
尝试这样的事情:
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")