Django CSV export
Question: I want to export the csv file form database, I have the following errors: that
say tuple index out of range, i don't know why

    
    
    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 
    

This is my 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
    


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

Statement `obj_all.filter(user_id = 3).values_list('contact_date')` return an
n * 1 dimension array. The length of `attr` is 1.

