Skip to content

Instantly share code, notes, and snippets.

@rg3915
Created September 2, 2016 20:44
Show Gist options
  • Save rg3915/6b8f748f467e39a009fcc35b04256cee to your computer and use it in GitHub Desktop.
Save rg3915/6b8f748f467e39a009fcc35b04256cee to your computer and use it in GitHub Desktop.
Export data with django-excel
''' Exporta planilhas em Excel '''
from datetime import datetime
import django_excel as excel
from django.http import HttpResponseBadRequest
from .models import Person
MDATA = datetime.now().strftime('%Y-%m-%d')
def export_data_person(request, atype):
if atype == "sheet":
return excel.make_response_from_a_table(
Person, 'xls', file_name="contatos")
elif atype == "custom":
query_sets = Person.objects.all()
column_names = ['id', 'name', 'email']
return excel.make_response_from_query_sets(
query_sets,
column_names,
'xls',
file_name="contatos-" + MDATA
)
else:
return HttpResponseBadRequest(
"Bad request. please put one of these " +
"in your url suffix: sheet, book or custom")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment