带有Django的AttributeError Rest嵌套的序列化程序

时间:2017-12-07 11:56:16

标签: python django serialization django-rest-framework

我按照Django Rest文档编写了嵌套的序列化程序,但它给了我属性错误。

以下是我的模特:

class Objects(TimeStampModel):
    projects = models.ForeignKey(Projects,related_name='proj_obj',on_delete=models.CASCADE)
    object_name = models.CharField(max_length=50)
    object_description = models.TextField()
    object_main_table = models.CharField(max_length=50)
    object_primary_key = models.CharField(max_length=50)
    object_age_field = models.CharField(max_length=50)
    date_format = models.CharField(max_length=50)

    def __str__(self):
        return self.object_name


class ObjectDefinition(TimeStampModel):
    ATTRIBUTE = 'Attribute'
    RELATION = 'Relation'
    TYPE_CHOICES = (
        (ATTRIBUTE, 'Attribute'),
        (RELATION, 'Relation'),
    )
    obj = models.ForeignKey(Objects,related_name='obj_def',on_delete=models.CASCADE)
    from_table = models.CharField(max_length=50)
    from_table_field = models.CharField(max_length=50)
    to_table = models.CharField(max_length=50)
    to_table_field = models.CharField(max_length=50)
    relation_type = models.CharField(max_length=50,choices=TYPE_CHOICES)
    relation_sequence = models.CharField(max_length=50)
    value_field = models.CharField(max_length=50, blank=True, null=True)

这是我的serializers.py片段:

class ObjectDefinitionSerializer(serializers.ModelSerializer):

    class Meta:
        model = ObjectDefinition
        fields = ('from_table','from_table_field','to_table','to_table_field','relation_type','value_field')

class ObjectSerializer(serializers.ModelSerializer):
    definition = ObjectDefinitionSerializer(many=True)
    object_description = serializers.CharField(required=False, allow_null=True, allow_blank=True
        )

    class Meta:
        model = Objects
        fields = ('projects','object_name','object_description','object_main_table','object_primary_key','object_age_field','date_format','definition')

    def validate(self, data, *args, **kwargs):
        date_format = data.get('date_format')
        if date_format not in ['YYYYMMDD', 'DDMMYYYY']:
            msg = ('Date format is incorrect')
            raise serializers.ValidationError({'error_msg': msg})
        return super(ObjectSerializer, self).validate(data, *args, **kwargs)

    def create(self, validated_data):
        definition_data = validated_data.pop('definition')
        obj = Objects.objects.create(**validated_data)
        for data in definition_data:
            ObjectDefinition.objects.create(obj=obj, **data)
        return obj

我的views.py:

class CreateObject(CreateAPIView):
    permission_classes = (IsAuthenticated,)
    serializer_class = ObjectSerializer

在点击POST后,objects.create适用于两个模型,但是在返回obj时,它会抛出这个错误:

例外值:

尝试在序列化程序definition上获取字段ObjectSerializer的值时,

出现AttributeError。 序列化程序字段可能名称不正确,并且与Objects实例上的任何属性或键都不匹配。 原始例外文字是:' Objects'对象没有属性'定义'。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

ObjectDefinition.obj的{​​{1}} related_nameobj_def,与您的序列化程序不匹配。

您可以通过提供source参数来解决这个问题:

definition = ObjectDefinitionSerializer(source='obj_def', many=True)