QueryDict不会从PATCH请求中分离json数据

时间:2019-12-28 21:46:05

标签: curl django-rest-framework

我正在尝试使用curl更新数据库中的对象:

curl -X PATCH http://127.0.0.1:8000/api/search-archive/?id_search=6  --header 'Content-Type:application/json' --data '{"archive":true}'

,但是QueryDict中的数据似乎无法正确拆分。打印request.data显示:

<QueryDict: {"'{archive:true}'": ['']}>

我的更新视图:

class SearchStopView(generics.UpdateAPIView):
    serializer_class = SearchSerializer
    def get_object(self,id_search):
        return Search.objects.get(id_search=id_search)

    def patch(self, request):
        print(request.data)

        id_search = self.request.query_params.get('id_search', None)
        search_object = self.get_object(id_search=id_search)
        serializer = SearchSerializer(search_object, data=request.data, partial=True) 

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST)

和网址代码:

url(r'^api/search-archive/$', SearchStopView.as_view(), name='search-archive')

我尝试了多种方式来更改curl和url,但没有任何效果...

0 个答案:

没有答案