Django CSV导出

时间:2014-05-22 05:55:45

标签: django

我想导出csv文件表单数据库, 我有以下错误:说元组索引超出范围,我不知道为什么

Request Method:     GET
Request URL:    http://www.article/export_excel/
Django Version:     1.6.2
Exception Type:     IndexError
Exception Value:    tuple index out of range

Exception Location:     /var/www/article/views.py in export_excel, line 191
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

['/usr/lib/python2.6/site-packages/pip-1.5.2-py2.6.egg',
 '/usr/lib64/python26.zip',
 '/usr/lib64/python2.6',
 '/usr/lib64/python2.6/plat-linux2',
 '/usr/lib64/python2.6/lib-tk',
 '/usr/lib64/python2.6/lib-old',
 '/usr/lib64/python2.6/lib-dynload',
 '/usr/lib64/python2.6/site-packages',
 '/usr/lib/python2.6/site-packages',
 '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info',
Server time:    Thu, 22 May 2014 14:45:02 +0900
Traceback Switch to copy-and-paste view
    /usr/lib/python2.6/site-packages/django/core/handlers/base.py in get_response
                            response = wrapped_callback(request, *callback_args, **callback_kwargs)
      ...
    ▶ Local vars
    /var/www/article/views.py in export_excel
                day = att[2].day
        ...
    ▶ Local vars 

这是我的views.py:

from datetime import datetime, time, date, timedelta
class workLog(object):
    def __init__(self, name, day, attTime, leaveTime):
        self.name = name
        self.day = day
        self.attTime = attTime
        self.leaveTime = leaveTime

def export_excel(request):
    from staffprofile.models import Myattendance,Myleavework
    response = HttpResponse(mimetype='application/vnd.ms-excel; charset="Shift_JIS"')
    response['Content-Disposition'] = 'attachment; filename=file.csv'
    writer = csv.writer(response)
    titles = ["No","name","day","attendance_time", "leave_time"
    writer.writerow(titles)
    obj_all = attendance.objects.filter(user_id = 3).values_list('user', 'contact_date', 'contact_time').order_by("-contact_date")
    lea = leavework.objects.filter(user_id = 3).values_list('contact_time').order_by('-contact_date')
    S = Staff.objects.all()
    row = [workLog('name', i, None, None) for i in range(32)]


    for att in obj_all.filter(user_id = 3).values_list('contact_date'):
        day = att[2]
        log = row[day]

        if log.attTime is None:
            log.attTime = att[2]
        elif log.attTime < att[2]:
            log.attTime = att[2]

    for leav in lea:
        day = leav[2].day
        log = row[day]
        if log.leaveTime is None:
            log.leaveTime = leav[2]
        elif log.leaveTime < leav[2]:
            log.leaveTime = leav[2]

    for log in row:
        if log.attTime is not None:
            if log.leaveTime is not None:
                row.append((log.attTime, log.leaveTime))
            else:
                row.append(None)
        else:
            if log.leaveTime is not None:
                row(None)

        writer.writerow(row)
    return response

1 个答案:

答案 0 :(得分:0)

https://docs.djangoproject.com/en/dev/ref/models/querysets/#values-list

语句obj_all.filter(user_id = 3).values_list('contact_date')返回一个n * 1维数组。 attr的长度为1.