首页 > 解决方案 > 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?

标签: pythondjangodjango-rest-framework

解决方案


Django 的官方文档中,您可以找到一个类似的示例。

在那个例子中,他们正在使用django.http.HttpResponse类,它也可以在你的情况下使用

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

推荐阅读