python - 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?
解决方案
从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
推荐阅读
- c# - 通过多个列的多个值列表进行 SQL 搜索
- javascript - 为什么 Chrome onerror 处理程序不返回完整的错误消息?
- python - 为什么 Django 迁移在每一行都使用相同的随机默认值?
- php - 将密钥文件添加到 PHP cURL 身份验证
- python - 混合比较两个数据框中的多个列并使用 python 添加一个新列 | 熊猫
- css - 使用带有 withStyles 和材质 ui 的通用类
- java - Apache Storm Kafka Spout 延迟问题
- c - 从C中的地址访问值
- qt - QML中的GridLayout如何在单元格中约束矩形
- google-maps - 谷歌通过一个 API 调用反向地理编码多个坐标?