Created
September 24, 2015 06:21
-
-
Save ncole458/62e50b121b508d3c0e13 to your computer and use it in GitHub Desktop.
Django REST Framework POST JSON & upload file based on request.data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FULL WORKING POST FOR JSON & FILE | |
def post(self, request, format=None): | |
up_file = request.data.get('file') | |
serializer = ridesSerializer(data=request.data) | |
if serializer.is_valid(): | |
serializer.save() | |
if up_file is not None: | |
if socket.getfqdn() == 'STG' or socket.getfqdn() == 'PRD': | |
destination = open(PRD_UPLOAD + up_file.name, 'wb+') | |
else: | |
destination = open('/Users/ncole/Documents/rides/ridesAPI/uploads/' + up_file.name, 'wb+') | |
for chunk in up_file.chunks(): | |
destination.write(chunk) | |
destination.close() | |
return Response(up_file.name, status=status.HTTP_201_CREATED) | |
return Response(serializer.data, status=status.HTTP_201_CREATED) | |
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment