码迷,mamicode.com
首页 > 其他好文 > 详细

django 大数量数据动态导出

时间:2020-07-14 13:30:52      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:open   django   rest   def   内容   csv文件   connect   util   csv   

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) + "\n"
        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) + "\n"
            else:
                cursor.close()
                break

django 大数量数据动态导出

标签:open   django   rest   def   内容   csv文件   connect   util   csv   

原文地址:https://www.cnblogs.com/niehongxu/p/13298357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!