0

That's a very naive question which means that I don't understand something very basic about how DRF works, but still: What is the way get a response from DRF in a form of text file containing json?

I have a ListAPIView:

class MyModelJSONView(generics.ListAPIView):
   serializer_class = MySerializer
   queryset = MyModel.objects.all()

I guess I should re-write the get method of this ListAPIView somehow to get a text file (I assume by adding Content-Disposition to a response. But how?

David Pekker
  • 333
  • 3
  • 12

1 Answers1

2
class MyModelJSONView(generics.ListAPIView):

    def get(self, request):

        filename = 'test.txt'
        queryset = MyModel.objects.all()
        serializer = MySerializer(queryset)
        response = HttpResponse(serializer.data, content_type='text/plain; charset=UTF-8')
        response['Content-Disposition'] = ('attachment; filename={0}'.format(filename))

        return response
Sergey Miletskiy
  • 477
  • 2
  • 5
  • 13