• 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
    
  • 相关阅读:
    如何去掉myeclipse的web项目启动时出现的 CodeLive Panel
    day35_Spring学习回顾_03
    在CentOS/RHEL上设置SSH免密码登录
    超全Linux备份工具集合,满足你的所有需要!
    使用Gnupg对Linux系统中的文件进行加密
    Linux压缩那些事儿
    或许是 Nginx 上配置 HTTP2 最实在的教程了
    Systemd 三部曲 之 PHP7
    Linux kernel模块管理相关详解
    搜狐视频Redis私有云平台CacheCloud
  • 原文地址:https://www.cnblogs.com/niehongxu/p/13298357.html
Copyright © 2020-2023  润新知