Skip to content Skip to sidebar Skip to footer

Django Rest Framework Upload File To A Method

So i have been trying to upload a file to a method using DRF with no luck so far. I was able to upload to a ModelViewSet using (FormParser, MultiPartParser,) with no problems, but

Solution 1:

Here is some code of what i did to overcome this problem. Although Kevin Brown answer probably works, i find my code a little "easier" approach:

    @detail_route(
        methods=['post', 'put', 'patch', 'get'], permission_classes=[AllowAny],
        authentication_classes=[NoAuthentication], serializer_class=MultimediaSerializer,
        parser_classes=(FormParser, MultiPartParser,)
    )
    def upload_avatar(self, request, pk=None):
        # Because we are using nested resources this was the only way i found to
        # upload a file. Maybe there is a better way
        if request.method in ['PATCH', 'POST']:
            avatar = request.FILES.get('avatar')
            if not avatar:
                return Response(status=404)

            try:
                woman = WomenNativePassport.objects.get(pk=pk)
            except WomenNativePassport.DoesNotExist:
                return Response(status=404)
            else:
                request.FILES['thumbnail'] = request.FILES['avatar']
                serializer = AvatarSerializer(
                    data=request.DATA, files=request.FILES
                )
                if serializer.is_valid():
                    woman.avatar.thumbnail.save(str(avatar), File(avatar))
                    return Response(status=204)
                else:
                    return Response(status=404)
        else:
            multimedia = Multimedia.objects.filter(user_profiles_avatares__pk=pk)
            page = self.paginate_queryset(multimedia)
            serializer = self.get_pagination_serializer(page)
            return Response(serializer.data)


# serializer 

class AvatarSerializer(serializers.Serializer):
    thumbnail = serializers.ImageField()

Solution 2:

Any uploaded files should be available in request.FILES, a dictionary keyed by the field that they was used when uploading. Once you have the file, it's a matter of handling it similar to any other uploaded file in Django.

If you can, I would use a second serializer that wraps the Multimedia model so the image validation and saving can be done automatically through Django REST Framework. There is an ImageField that will automatically validate the image by Pillow which you can use on the serializer.


Post a Comment for "Django Rest Framework Upload File To A Method"