Django export the CSV file of objects form databas

57 views
Skip to first unread message

hito koto

unread,
May 21, 2014, 3:21:25 AM5/21/14
to django...@googlegroups.com

Hello,

I have the following errors: why append is not done?

AttributeError at /export_excel/

'int' object has no attribute 'append'
Request Method: GET
Request URL: http://article/export_excel/
Django Version: 1.6.2
Exception Type: AttributeError
Exception Value:
'int' object has no attribute 'append'
Exception Location: /var/www/html/staff/views.py in export_excel, line 181
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',
 '/var/www/html/ihttest/ihttcs_test/kojin',
 '/var/www/html/ihttest/ihttcs_test/kojin/static/']
Server time: Wed, 21 May 2014 12:15:09 +0900

Traceback Switch to copy-and-paste view

  • /usr/lib/python2.6/site-packages/django/core/handlers/base.py in get_response
    1.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
      ...
  • /var/www/html/staff/views.py in export_excel
    1.         row[0][0].append(a)
      ...


This is my views.py:

def export_excel(request):
    response = HttpResponse(mimetype='application/vnd.ms-excel; charset="Shift_JIS"')
    response['Content-Disposition'] = 'attachment; filename=file.csv'
    writer = csv.writer(response)
    titles = ["No","名前".encode("Shift_JIS"),"日付".encode("Shift_JIS"),"出勤時間".encode("Shift_JIS"), "退勤>時間".encode("Shift_JIS")]
    writer.writerow(titles)
    obj_all = Myattendance.objects.filter(id = 1).values_list('user', 'contact_date', 'contact_time').order_by("-contact_time")
    lea = Myleavework.objects.filter(id = 1).values_list('contact_time').order_by('-contact_time')

    row = [[0 for i in range(5)] for i in range(31)]
    for a in obj_all.filter().values_list('user_id'):
        row[0][0].append(a)
    for b in obj_all.filter().values_list('contact_date'):
        row[0][1].append(b)
    for c in obj_all.filter().values_list('contact_time'):
        row[0][2].aapend(c)

        writer.writerow(row)
    return response



Erik Cederstrand

unread,
May 21, 2014, 6:04:09 AM5/21/14
to Django Users
Den 21/05/2014 kl. 05.21 skrev hito koto <hitoko...@gmail.com>:

> Hello,
>
> I have the following errors: why append is not done?
>
> row = [[0 for i in range(5)] for i in range(31)]

Here you are creating a list of lists of 0's (a 2-dimensional matrix of ints).

> for a in obj_all.filter().values_list('user_id'):
> row[0][0].append(a)

Here, row[0][0] points to an int. You can't append to an int. Without knowing what you want to achieve, either initialize your list of lists with lists instead of 0's, or do "row[0][0] += a" instead.

> for b in obj_all.filter().values_list('contact_date'):
> row[0][1].append(b)
> for c in obj_all.filter().values_list('contact_time'):
> row[0][2].aapend(c)

Spelling error.

Erik

hito koto

unread,
May 21, 2014, 9:25:16 AM5/21/14
to django...@googlegroups.com
Ok, thank you!

2014年5月21日水曜日 15時04分09秒 UTC+9 Erik Cederstrand:
Reply all
Reply to author
Forward
0 new messages