Admin action for a generic "CSV Export"

 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
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
        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('.', '_')

        writer = csv.writer(response)
        if header:
            writer.writerow(list(field_names))
        for obj in queryset:
            writer.writerow([unicode(getattr(obj, field)).encode("utf-8","replace") for field in field_names])
        return response
    export_as_csv.short_description = description
    return export_as_csv

More like this

  1. Admin action for a generic "CSV Export" (fix for unicode) by __alexander__ 4 months, 3 weeks ago
  2. Generic CSV export admin action factory with labels by losttrekker 2 years, 1 month ago
  3. Generic CSV export admin action factory with relationship spanning fields and labels by blackrobot 1 year, 4 months ago
  4. Admin Download as CSV File by msaron 2 years, 2 months ago
  5. Generic admin action export selected rows to excel by jordic 2 years, 6 months ago

Comments

macmind (on August 18, 2011):

This might help for folks that need this ordered by the field list given in the admin module:

writer = csv.DictWriter(response,fields)
writer.writeheader()

for obj in queryset:
    writer.writerow(dict(zip(fields,[unicode(getattr(obj, field)).encode("utf-8","replace") for field in fields])))

#

allaby (on November 14, 2011):

I need ordering preserved as mentioned by macmind, but this I'm getting the error "dictionary update sequence element #0 has length 1; 2 is required".

Does anyone have any thoughts on how I can fix this?

#

tatlar (on January 29, 2014):

macmind's code works, but you must use the code macmind posted exactly. So, replace:

writer = csv.writer(response)

if header:

writer.writerow(list(field_names))

with:

writer = csv.DictWriter(response,fields)

writer.writeheader()

and also notice that the

for obj in queryset:

loop uses fields not field_names. These are critical modifications.

#

tatlar (on February 7, 2014):

Note that the writer.writeheader() method is only available in Python 2.7+. Python 2.6 and lower do not support this and you will get an AttributeError exception thrown.

#

(Forgotten your password?)