Django csv花了很长时间输出CSV

时间:2017-05-12 13:34:32

标签: python django csv

我有一个大约90,000的文本条目的模型,我输出Django CSV,但它没有转换输出CVS我离开浏览器半小时a和没有输出。但我使用它的方法工作正常数据是低。

我的方法: -

def usertype_csv(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="university_list.csv"'
    writer = csv.writer(response)
    news_obj = Users.objects.using('cms').all()
    writer.writerow(['NAME', 'USERNAME', 'E-MAIL ID','USER TYPE','USER TYPE'])
    for item in news_obj:
        writer.writerow([item.name.encode('UTF-8'),item.username.encode('UTF-8'),item.email.encode('UTF-8'),
                             item.userTypeId.userType.encode('UTF-8'),item.universityId.name.encode('UTF-8')])


    return response

我测试了它可以处理的较小数据,但对于非常大的文件,它无法正常工作。 提前致谢

2 个答案:

答案 0 :(得分:0)

您生成的任何大型CSV文件理想情况下应为streamed back to the user

答案 1 :(得分:0)

Here你可以找到一个关于django如何处理它的例子。 我就是这样做的:

import csv
from django.http import StreamingHttpResponse

class Echo(object):
    """An object that implements just the write method of the file-like
    interface.
    """
    def write(self, value):
        """Write the value by returning it, instead of storing in a buffer."""
        return value

def some_streaming_csv_view(request):
    """A view that streams a large CSV file."""
    # Generate a sequence of rows. The range is based on the maximum number of
    # rows that can be handled by a single sheet in most spreadsheet
    # applications.
    rows = (["Row {}".format(idx), str(idx)] for idx in range(165536))
    pseudo_buffer = Echo()
    news_obj = Users.objects.using('cms').all()
    writer = csv.writer(pseudo_buffer)
    response = StreamingHttpResponse((writer.writerow([item.name.encode('UTF-8'), item.username.encode('UTF-8'), item.email.encode('UTF-8'), item.userTypeId.userType.encode('UTF-8'), item.universityId.name.encode('UTF-8')]) for item in news_obj), content_type="text/csv")
    response['Content-Disposition'] = 'attachment; filename="university_list.csv"'
    return response