django 大数量数据动态导出

django 大数量数据导出

下载指定文件

# 一般我们下载指定文件时可使用如下方法。
def down_load(path, file_name):
    f = open(path, "rb")
    response = FileResponse(f)
    response['Content-Type'] = "application/octet-stream"
    disposition = 'attachment;filename={}.xlsx'.format(escape_uri_path(file_name))
    response['Content-Disposition'] = disposition
    return response

当我们想实现动态从数据库查询并下载大文件时这个方法就不太可行。

查询数据库并实现大数据导出

以生成csv文件并下载举例

- 借助 django的:
		StreamingHttpResponse,一个以迭代器为响应内容的流式HTTP响应类
		escape_uri_path,解决中文文件名乱码

上代码:

from django.db import connections
from django.utils.encoding import escape_uri_path
from django.http.response import StreamingHttpResponse
from rest_framework.generics import GenericAPIView


class OutPutTestView(GenericAPIView):

    def get(self, request):
		
        response = StreamingHttpResponse(self.download_main())
        response['Content-Type'] = "application/octet-stream;charset=gbk"
        disposition = 'attachment;filename={}.csv'.format(escape_uri_path("测试"))
        response['Content-Disposition'] = disposition
        return response

    def download_main(self):

        title = ["id", "姓名", "电话", "性别", "失效时间"]
        # 生成标题
        yield ",".join(title) + "
"
        cursor = connections["default"].cursor()
        sql = "select id, nickname, phone, gender, expire_time from employee_record"
        cursor.execute(sql)
        while True:
            stream = cursor.fetchone()
            if stream:
                stream = [str(info) for info in stream]
                yield ",".join(stream) + "
"
            else:
                cursor.close()
                break
原文地址:https://www.cnblogs.com/niehongxu/p/13298357.html