0

使用REST Framework,在进行 POST 时出现以下错误...

TypeError at /api/profiles/
'attribute_answers' is an invalid keyword argument for this function

PUT 似乎没有任何问题。

串行器

class ProfileSerializer(serializers.ModelSerializer):
    user = serializers.SlugRelatedField(slug_field='username')
    attribute_answers = serializers.PrimaryKeyRelatedField(many=True)

    class Meta:
        model = Profile
        depth = 2
        fields = ('id', 'name', 'active', 'type', 'user', 'attribute_answers')

    def restore_object(self, attrs, instance=None):
        """
        Create or update a new snippet instance.

        """
        if instance:
            # Update existing instance
            instance.name = attrs.get('name', instance.name)
            instance.active = attrs.get('active', instance.active)
            instance.type = attrs.get('type', instance.type)
            instance.attribute_answers = attrs.get('attribute_answers', instance.attribute_answers)
            return instance

        # Create new instance
        return Profile(**attrs)
4

1 回答 1

3

您的restore_object方法错误地尝试传递attribute_answersProfile构造函数。

碰巧的是,由于您正在使用ModelSerializer您根本不需要该restore_object方法 - 模型实例恢复将为您处理。该restore_object方法仅对基本Serializer类是必需的。

于 2013-02-20T10:38:01.350 回答