2017-02-24 10 views
0

REST APIにjqueryを使用してPUT呼び出しを実行しようとすると、何らかの理由で以下のエラーが表示されます。Django Rest Framework - 通常のPUT jsonデータから返されるFIleUploadの解析エラー

{"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(){ 
      } 
     }); 

シリアライザ:

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

答えて

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) 
関連する問題