Django Rest Framework - 正常的PUT json数据返回FIleUpload解析错误

时间:2017-02-24 11:36:51

标签: jquery django django-rest-framework

出于某种原因,当我尝试使用jquery对我的REST API进行PUT调用时,我收到以下错误。

{"detail":"FileUpload parse error - none of upload handlers can handle the stream"}

这是观点:

class GateView(APIView):
    def get_object(self, pk):
        try:
            return Gate.objects.get(pk=pk)
        except Gate.DoesNotExist:
            raise Http404
    def get(self, request, coords, format=None):
        results = []
        try:
            pa_distances = []
            for pa in Gate.objects.all():
                point = Point(float(coords.split(',')[0]),float(coords.split(',')[1]))
                polygon = shape(pa.polygone['features'][0]['geometry'])
                if polygon.contains(point):
                    results = [pa]
                    break

        except Exception, e:
            print e
            pass
        serializer = GateSerializer(results, many=True)
        return JSONResponse(serializer.data)

    def put(self, request, coords, format=None):
        gate = self.get_object(coords)
        print gate
        serializer = GateSerializer(gate, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

jQuery的:

$.ajax({
    url: '/protected-area/' + type + '/' + pk + '/',    //Your api url
    type: 'PUT',   //type is any HTTP method
    data: {name: feature.getProperties().name},      //Data as js object
    success:function(){
    }
});

Serialiser:

class GateSerializer(serializers.ModelSerializer):
    protected_area = serializers.ReadOnlyField(source='protected_area.name')
    class Meta:
        model = Gate
        fields = ('protected_area', 'name')

1 个答案:

答案 0 :(得分:0)

def put(self, request, coords, format=None):
        gate = self.get_object(coords)
        print gate
        serializer = GateSerializer(instance=gate, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)