[Answered ]-A multipart/form-data with nested serializers and files, DRF raises "The submitted data was not a file. Check the encoding type on the form."

1๐Ÿ‘

โœ…

Iโ€™ve come to a solution, which is not the desired for me but it works.

I have discovered that for any unknown reason, the file/image field is being inserted into a list if I was using my MultiPartJSONParser.

To solve it I have to extract this fields from the list in the views.py before calling super().create() or super().update()

views.py

class CreateProfileView(CreateAPIView):
    serializer_class = ProfileSerializer
    parser_classes = [MultiPartJSONParser]

    def create(self, request, *args, **kwargs):
        for key in request.FILES:
            request.data[key] = request.data[key][0]
        return super().update(request, *args, **kwargs)

class RUDProfileView(RetrieveUpdateDestroyAPIView):
    serializer_class = ProfileSerializer
    queryset = UserProfile.objects.all()
    lookup_field = 'user'
    parser_classes = [MultiPartJSONParser]

    def update(self, request, *args, **kwargs):
        for key in request.FILES:
            request.data[key] = request.data[key][0]
        return super().update(request, *args, **kwargs)

๐Ÿ‘คafinkado

Leave a comment