django 大數量資料動態匯出
阿新 • • 發佈:2020-07-14
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