django-rest-framework-csv icon indicating copy to clipboard operation
django-rest-framework-csv copied to clipboard

CSVStreamingRenderer Example

Open ghost opened this issue 8 years ago • 2 comments

Hey there, I am trying to implement a streaming CSV renderer within a list API view and am having trouble with the code provided in renderers.py. Is it possible to get a full example in example/views.py that shows its usage?

ghost avatar Oct 11 '17 20:10 ghost

Here is how I use it:

class CSVStreamingView(APIView):
    renderer_classes = [CSVStreamingRenderer]
    def get(self, request, format=None):
        if format == "csv":
            request.accepted_renderer.header = [
                'keya',
                'keyb'
            ]
            data = [ {'keya': 3, 'keyb': 4}, {'keya': 6, 'keyb': 7} ]
            response = StreamingHttpResponse(
                request.accepted_renderer.render(data),
                content_type="text/csv")
            return response        

jrzerr avatar Nov 02 '17 17:11 jrzerr

Awesome, thank you. It turned out that it was more an issue with the queryset not being paged rather than django CSV renderer. I'll pop my solution here.

ghost avatar Nov 02 '17 23:11 ghost