嵌套的序列化程序,多个表不起作用

时间:2017-11-13 10:09:32

标签: django python-3.x django-rest-framework

我在models.py中有以下表格。

class ProductLine(models.Model):
    availability = models.CharField(max_length=20, blank=True, null=True)
    series = models.CharField(max_length=20, blank=True, null=True)
    model = models.CharField(max_length=20, blank=True, null=True)

    class Meta:
        db_table = "product_line"

class DriveType(models.Model):
    drive_name = models.CharField(max_length=20, blank=True, null=True)    
    product_line = models.ForeignKey(ProductLine, related_name="drive_type")

class Requirements(models.Model):
    performance_unit = models.CharField(max_length=100, blank=True, null=True)
    drive_type = models.OneToOneField(DriveType,on_delete=models.CASCADE,primary_key=True)

    class Meta:
        db_table = "requirements"

class WorkloadType(models.Model):
    workload_type_options = models.CharField(max_length=50, blank=True, null=True)    
    drive_type = models.OneToOneField(DriveType,on_delete=models.CASCADE,primary_key=True)

    class Meta:
        db_table = "workload_type"

我有以下序列化器:

class WorkloadTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = WorkloadType
        fields = "__all__"


class RequirementsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Requirements
        fields = "__all__"


class DriveTypeSerializer(serializers.ModelSerializer):
    requirements = RequirementsSerializer(many = False, read_only = True)
    workload_type = WorkloadTypeSerializer(many=False,read_only=True)

    class Meta:
        model = DriveType
        fields = (
        "drive_name", "available_drive_type", "capacity", "raid_type", "raid_size", "workload", "workload_percentage",
        "raid_groups", "compression", "compression_value","requirements","workload_type")


class ProductLineSerializer(serializers.ModelSerializer):
    drive_type = DriveTypeSerializer(many=True, read_only=True)

    class Meta:
        model = ProductLine
        fields = ('availability','series','model','drive_type')

在我看来,我有这个:

class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
    def get_queryset(self):
        return ProductLine.objects.filter(id=self.kwargs.get("pk"))
    serializer_class = ProductLineSerializer

我的输出如下:

{
    "availability": "Current",
    "series": "3000",
    "model": "2000",
    "drive_type": [
        {
            "drive_name": "drive1",
            "requirements": {
                "drive_type": 2,
                "performance_unit": "by_iops",
            }
        }
    ]
}

为什么我无法在json中看到WorkLoadType表数据,因为我能够看到需求数据。我甚至不能在json看到这个领域。嵌套序列化程序仅支持单个关系表

1 个答案:

答案 0 :(得分:0)

回答我自己的问题。必须在模型中使用related_name来反向关系。

class Requirements(models.Model):
    performance_unit = models.CharField(max_length=100, blank=True, null=True)
    drive_type = models.OneToOneField(DriveType,on_delete=models.CASCADE,primary_key=True,related_name=requirements)

    class Meta:
        db_table = "requirements"

class WorkloadType(models.Model):
    workload_type_options = models.CharField(max_length=50, blank=True, null=True)    
    drive_type = models.OneToOneField(DriveType,on_delete=models.CASCADE,primary_key=True,related_name=workload_type)