如何在Django Rest框架中序列化多个关系模型

时间:2018-09-09 14:53:49

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

我是这个Django rest框架的新手,我正在尝试创建一个API。我的model.py如下所示

>>>a
1
>>>b
2
>>>c
3
>>>d
4

和serializers.py看起来像

class FieldTypes(models.Model):
    field_type = models.CharField(max_length=255, null=False)
    deleted = models.BooleanField(default=False)

class Forms(models.Model):
    form_name = models.CharField(max_length=255, null=False)
    deleted = models.BooleanField(default=False)

class FormFields(models.Model):
    form = models.ForeignKey(Forms, related_name = 'form_fields', on_delete=models.CASCADE)
    field_type = models.ForeignKey(FieldTypes, related_name = 'form_field_types', on_delete=models.CASCADE)
    description = models.CharField(max_length=255, null=False)
    deleted = models.BooleanField(default=False)

class FormFieldOptions(models.Model):
    form_field = models.ForeignKey(FormFields, related_name = 'form_field_options', on_delete=models.CASCADE)
    description = models.CharField(max_length=255, null=False)
    deleted = models.BooleanField(default=False)

这样我就能产生类似

的结果
class FormFieldSerializer(serializers.ModelSerializer):
    form_field_options = FormFieldOptionsSerializer(many=True)
    class Meta:
        model = FormFields
        fields = ('id', 'description', 'form_field_options')

class FormSerializer(serializers.ModelSerializer):
    form_fields = FormFieldSerializer(many=True)
    class Meta:
        model = Forms
        fields = ('form_name', 'form_fields')

class FieldTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = FieldTypes
        fields = ('field_type')

但是我需要知道表单字段属于哪种字段类型。而且我不确定如何在JSON数据中引入字段类型。请指导我获得预期的结果。请让我知道是否应该改革数据模型以获得预期结果。

跟踪:

{
    "form_name": "Ticket",
    "form_fields": [
        {
            "id": 1,
            "description": "Type",
            "form_field_options": [
                {
                    "id": 1,
                    "description": "Question"
                },
                {
                    "id": 2,
                    "description": "Incident"
                },
                {
                    "id": 3,
                    "description": "Problem"
                },
                {
                    "id": 4,
                    "description": "Task"
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:2)

field_type 中将 FormFieldSerializer 字段添加为

class FormFieldSerializer(serializers.ModelSerializer):
    form_field_options = FormFieldOptionsSerializer(many=True)
    class Meta:
        model = FormFields
        fields = ('id', 'description', 'form_field_options','field_type')

这将返回FieldType 实例的 PK。


如果要显示 字符串表示形式 ,请在下面的代码段中使用

class FormFieldSerializer(serializers.ModelSerializer):
    form_field_options = FormFieldOptionsSerializer(many=True)
    field_type = serializers.CharField(source='field_type.field_type')

    class Meta:
        model = FormFields
        fields = ('id', 'description', 'form_field_options', 'field_type')