Django Rest Framework:序列化器响应错误

我在 DRF 中的视图集有问题:


def get_queryset(self):

    """

    :return: filtered queryset based on request query_param


    """

    # todo#amedeo: improve the readability code

    qs = super(ChecklistViewSet, self).get_queryset()

    _pk = self.kwargs['pk']


    if self.request.method == 'PUT':

        instance = qs.filter(pk=int(_pk)).first()

        # pass in the instance we want to update

        serializer = ChecklistSerializer(instance, self.request.data)


        # validate and update

        if serializer.is_valid():

            serializer.save()

            serializer_dict = serializer.data

            serializer_dict["message"] = "Checklist updated successfully."

            return response.Response(serializer_dict, status=status.HTTP_200_OK)


        else:

            return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

在我的代码中,objetc 被保存,但响应给出了一个错误,说:


AttributeError: 'Response' 对象没有属性 'model'


我的序列化程序是:


class ChecklistSerializer(serializers.ModelSerializer):


class Meta:

    model = Checklist

    fields = ('id', 'approved_by', 'role', 'hardship_classification',

              'total_capacity', 'capacity_for_residents', 'checklist_type',

              'state', 'pdf', 'submitting_afp', 'disabled', 'location')

我通过传递 json 的客户端 PUT 方法调用:


{

"approved_by": "Test",

"role": "test_role_4",

"hardship_classification": "test_6",

"total_capacity": "50",

"capacity_for_residents": "350",

"checklist_type": "permanent",

"state": "qcl_draft",

"pdf": null,

"submitting_afp": 3999,

"disabled": false,

"location": 97

}


临摹微笑
浏览 262回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python