Skip to content

Instantly share code, notes, and snippets.

@vinaychittora
Last active December 27, 2015 15:58
Show Gist options
  • Save vinaychittora/bd76875d0ba84b440140 to your computer and use it in GitHub Desktop.
Save vinaychittora/bd76875d0ba84b440140 to your computer and use it in GitHub Desktop.
Generic admin action to export data into excel/csv
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse, HttpResponseRedirect
from pyExcelerator import *
from django.contrib.admin.util import lookup_field
from django.utils.html import strip_tags
from django.contrib import messages
def export_as_xls(modeladmin, request, queryset):
"""
Generic xls export admin action.
"""
if queryset.count()>settings.EXPORT_RECORDS_LIMIT:
messages.error(request, "Can't export more then %s Records in one go. Narrow down your criteria using filters or search" % str(settings.EXPORT_RECORDS_LIMIT))
return HttpResponseRedirect(request.path_info)
fields = []
#PUT THE LIST OF FIELD NAMES YOU DON'T WANT TO EXPORT
exclude_fields = []
#foreign key related fields
extras = ['']
if not request.user.is_staff:
raise PermissionDenied
for f in modeladmin.list_display:
if f not in exclude_fields:
fields.append(f)
fields.extend(extras)
opts = modeladmin.model._meta
wb = Workbook()
ws0 = wb.add_sheet('0')
col = 0
field_names = []
# write header row
for field in fields:
ws0.write(0, col, field)
field_names.append(field)
col = col + 1
row = 1
# Write data rows
for obj in queryset:
col = 0
for field in field_names:
if field in extras:
try:
val = [eval('obj.'+field)] #eval sucks but easiest way to deal
except :
val = ['None']
else:
try:
val = lookup_field(field, obj, modeladmin)
except :
val = ['None']
ws0.write(row, col, str(strip_tags(val[-1])).strip())
col = col + 1
row = row + 1
wb.save('/tmp/output.xls')
response = HttpResponse(open('/tmp/output.xls','r').read(),
mimetype='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename=%s.xls' % unicode(opts).replace('.', '_')
return response
export_as_xls.short_description = "Export selected to XLS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment