Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django rest framework return file

Tags:

I have the following view in my views.py -

class FilterView(generics.ListAPIView):     model = cdx_composites_csv      def get(self, request, format=None):         vendor = self.request.GET.get('vendor')         filename = self.request.GET.get('filename')         tablename = filename.replace(".","_")         model = get_model(vendor, tablename)         filedate = self.request.GET.get('filedate')                 snippets = model.objects.using('markitdb').filter(Date__contains=filedate)         serializer = cdx_compositesSerializer(snippets, many=True)         if format == 'raw':             zip_file = open('C:\temp\core\files\CDX_COMPOSITES_20140626.zip', 'rb')             response = HttpResponse(zip_file, content_type='application/force-download')             response['Content-Disposition'] = 'attachment; filename="%s"' % 'CDX_COMPOSITES_20140626.zip'             return response          else:             return Response(serializer.data) 

It works great for xml, json, csv but when I try to use raw it's not returning the file instead it's giving ""detail": "Not found"" why is this happening?

The URL I'm hitting is as follows -

json example that works -

http://dt-rpittom:8000/testfilter/?vendor=markit&filename=cdx_composites.csv&filedate=2014-06-26&format=json

This should return a zip file for download.

http://dt-rpittom:8000/testfilter/?vendor=markit&filename=cdx_composites.csv&filedate=2014-06-26&format=raw

like image 832
whoisearth Avatar asked Aug 22 '14 14:08

whoisearth


People also ask

What is JSONRenderer in Django?

Second thing is, you have to return serializer. data back to client in form of JSON, not dictionary. Thats what JSONRenderer does. It convert dict to JSON and its implemented inside Response class https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/response.py.

How do I upload an Excel file in Django REST framework?

So you have two choices: let ModelViewSet and ModelSerializer handle the job and send the request using content-type=multipart/form-data; set the field in ModelSerializer as Base64ImageField (or) Base64FileField and tell your client to encode the file to Base64 and set the content-type=application/json.


1 Answers

I don't know why I had to do this - may be something internal to Django Rest Framework that doesn't allow putting custom methods onto format?

I simply changed it to the following -

if fileformat == 'raw':     zip_file = open('C:\temp\core\files\CDX_COMPOSITES_20140626.zip', 'rb')     response = HttpResponse(FileWrapper(zip_file), content_type='application/zip')     response['Content-Disposition'] = 'attachment; filename="%s"' % 'CDX_COMPOSITES_20140626.zip'     return response 

Then in my URL just hit with the new value and it works fine. I'd love to know why I can't use format though to serve a file.

like image 128
whoisearth Avatar answered Oct 04 '22 04:10

whoisearth