Skip to content

Instantly share code, notes, and snippets.

@titovanton
Created July 28, 2020 20:29
Show Gist options
  • Save titovanton/1a6b2a47d5ab74b1db5f28962af80a06 to your computer and use it in GitHub Desktop.
Save titovanton/1a6b2a47d5ab74b1db5f28962af80a06 to your computer and use it in GitHub Desktop.
class FileUploadSerializer(serializers.Serializer):
attachment = serializers.FileField()
class FileUploadView(views.APIView):
'''
FileUploadParser - is some sort of retardnes,
it takes entier http body and parses it as a file 0_0.
Very useful! Specially trash headers, prepended to the text file...
So, just don't use it ;-> Use MultiPartParser instead.
'''
parser_classes = [MultiPartParser]
# in this case, we need that only for standard DRF HTML pages,
# which uses a client, when visits an endpoint.
serializer_class = FileUploadSerializer
def put(self, request, **kwargs):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
file_obj = request.FILES['attachment']
reader = csv.reader(file_obj.file)
# ...
# do fun with file
# ...
return Response({}, status=status.HTTP_201_CREATED)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment