Skip to content

Instantly share code, notes, and snippets.

@ncole458
Created September 24, 2015 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncole458/62e50b121b508d3c0e13 to your computer and use it in GitHub Desktop.
Save ncole458/62e50b121b508d3c0e13 to your computer and use it in GitHub Desktop.
Django REST Framework POST JSON & upload file based on request.data
# 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