-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSV_Export.py
46 lines (43 loc) · 1.79 KB
/
CSV_Export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import csv
from django.http import HttpResponse
def export_as_csv_action(description="Export selected objects as CSV file",
fields=None, exclude=None, header=True):
"""
This function returns an export csv action
'fields' and 'exclude' work like in django ModelForm
'header' is whether or not to output the column names as the first row
"""
def export_as_csv(modeladmin, request, queryset):
"""
Generic csv export admin action.
based on http://djangosnippets.org/snippets/1697/
"""
opts = modeladmin.model._meta
#field_names = set([field.name for field in opts.fields])
#if fields:
# fieldset = set(fields)
# #field_names = field_names & fieldset
# field_names = fieldset
#elif exclude:
# excludeset = set(exclude)
# field_names = field_names - excludeset
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % unicode(opts).replace('.', '_')
response['ContentType'] = 'application/ms-excel';
response['ContentEncoding'] = "big5";
writer = csv.writer(response)
#writer.writerow(unicode('\xef\xbb\xbf').encode("utf-8","replace"))
if header:
writer.writerow(list(fields))
for obj in queryset:
rowdata=list()
for field in fields:
if hasattr(getattr(obj, field), '__call__'):
data=getattr(obj, field)()
else:
data=getattr(obj, field)
rowdata.append(unicode(data).encode("big5","replace"))
writer.writerow(rowdata)
return response
export_as_csv.short_description = description
return export_as_csv