I have managed to use the Django REST-framework to upload an image 
associated with an item. However, the PUT request throws an error (400) 
when attempting to update the image. Here is the code: 

   - Model: 
      - image = models.ImageField(upload_to="images/", blank=True, 
      null=True)
   - Serializer: 
      - class ItemSerializer(serializers.ModelSerializer):
      
      class Meta:
      
      model = Item
      
      fields = "__all__"
      
      

   - View:
   - @api_view(["GET", "PUT"])
   def api_item_beta(request, pk):
   
   try:
   
   item = Item.objects.get(pk=pk)
   
    except Item.DoesNotExist:
   
   return Response(status=status.HTTP_404_NOT_FOUND)
   
   if request.method == "GET":
   
   serializer = ItemSerializer(item)
   
   return Response(serializer.data)
   
   elif request.method == "PUT":
   
   serializer = ItemSerializer(item, 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)
   
   
So, I can POST an image using this serializer. The PUT request works for 
everything EXCEPT the image. The image data is identical in both cases 
(where I am posting and putting). What am I missing that the serializer is 
expecting??

Any help much appreciated! 

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/0d8c2a9d-b3fc-4bae-b8ab-36120e9775a2n%40googlegroups.com.

Reply via email to