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?

2
  • A similar question was asked and answered here. Commented Apr 2, 2018 at 1:50
  • You can always save the JSON response to a text file. Commented Apr 2, 2018 at 9:56

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.