Skip to content

Instantly share code, notes, and snippets.

@vb64
Created July 21, 2022 19:17
Show Gist options
  • Select an option

  • Save vb64/f80d655d04ca1d197517b082bdfbae8f to your computer and use it in GitHub Desktop.

Select an option

Save vb64/f80d655d04ca1d197517b082bdfbae8f to your computer and use it in GitHub Desktop.
from django.contrib.admin.views.main import ChangeList
...
def csv_download(self, request):
"""View for data download taking into account custom filters and sortings."""
clist = ChangeList(
request,
self.model,
self.list_display,
self.list_display_links,
self.list_filter,
self.date_hierarchy,
self.search_fields,
self.list_select_related,
self.list_per_page,
self.list_max_show_all,
self.list_editable,
self,
self.sortable_by
)
return HttpResponse(
queryset2csv(clist.get_queryset(request)),
headers={
'Content-Type': 'text/csv',
'Content-Disposition': 'attachment; filename="data.csv"',
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment