zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    Linux文件系统命令 cd
    Linux文件系统命令 cat
    正则表达式(.+?)与(.+)区别
    Linux文件系统命令 ls
    如何正确认识Docker Kubernetes 和 Apache Mesos
    基于Nutch Solr等基于搭建一体化的数据抓取平台
    ubuntu apache ssl配置
    机器学习入门
    docker 安全性问题
    数据工程师面试必备——Python与数据库的那些事
  • 原文地址:https://www.cnblogs.com/niehongxu/p/13298357.html
Copyright © 2011-2022 走看看