我有一个用例,我必须生成并发送记录高达 4M 的 Excel 文件,因为该文件可能很大,我想在生成文件时流式传输响应
https://docs.djangoproject.com/en/2.0/howto/outputting-csv/#streaming-large-csv-files:这正是我想要的,但是对于记录高达4M的Excel文件
这是我想以excel格式实现的csv格式的模板代码
def get_stuff():
def fetch_from_server_cursor(cursor, cursor_name, fetch_size=10000):
while True:
cursor.execute("FETCH {fetch_size} FROM {cursor_name}".format(fetch_size = fetch_size, cursor_name = cursor_name))
chunk = cursor.fetchall()
if not chunk:
return
yield from chunk
with transaction.atomic(), connection.cursor() as cursor:
cursor_name = "my_cursor"
cursor.execute(
"""
DECLARE {cursor_name} CURSOR FOR
SELECT field1,field2
FROM {table_name}
WHERE field3 = XX
""".format(cursor_name = cursor_name,table_name = MyModel.objects.model._meta.db_table)
)
yield from fetch_from_server_cursor(cursor, cursor_name)
def stream_csv_view(request):
"""A view that streams a large CSV file."""
# Generate a sequence of rows. The range is based on the maximum number of
# rows that can be handled by a single sheet in most spreadsheet
# applications.
pseudo_buffer = Echo()
writer = csv.writer(pseudo_buffer)
response = StreamingHttpResponse((writer.writerow(row) for row in get_stuff()),
content_type="text/csv")
response['Content-Disposition'] = 'attachment; filename="test.csv"'
return response
有人可以研究一下
get_stuff()
功能并推荐我一种更好的方法来访问数据库,这个失败了
有什么帮助吗?
您可以使用
django-queryset-csv
模块从查询集中导出 csv,它默认使用 StreamingHttpResponse 而不是普通的 HttpResponse。
这个案例你找到答案了吗?