Q:

Better way to file response with DRF?

I have this action:
@action(methods=['get'], detail=True)
    def download_csv(self, request, pk, *args, **kwargs):
        project = self.get_object()
        data = show_stages_tasks(request, pk)
        file_name = f"{project.name}.csv"
        export_to_csv(data, file_name)

        file_handle = open(file_name, "r")
        response = FileResponse(file_handle.read(), content_type='application/csv')
        response['Content-Disposition'] = f'attachment; filename="{file_handle.name}"'
        file_handle.close()
        os.remove(file_name)
        return response

and export_to_csv is:
def export_to_csv(data, filename="project"):    
    content = JSONRenderer().render(data)
    stream = io.BytesIO(content)
    content_parsed = JSONParser().parse(stream)

    tasks = content_parsed[0]["related_tasks"]

    keys = tasks[0].keys()
    with open(filename, 'w') as output_file:
        dict_writer = csv.DictWriter(output_file, fieldnames=keys)
        dict_writer.writeheader()
        for task in tasks:
            task['children'] = []
            task['task_folders'] = []
            dict_writer.writerow(task)

And show_stages_tasks returns a serialized data with DRF serializer, with 3 nested serializers (too big and I think unnecessary to post it here).
As you see here - I parse serializer data, create a CSV file, save it, next open it, pass in the Response and delete file. The question is can I somehow pass the content of the file, without actually creating CSV file and next deleting it?

A:

From the Django's official doc, you could find a similar example.
In that example, they are using django.http.HttpResponse class, it can be also used in your case

import csv 
from django.http import HttpResponse

def some_view(request):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'

    writer = csv.writer(response)
    writer.writerow(['First row', 'Foo', 'Bar', 'Baz'])
    writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"])

    return response

