Skip to content

Instantly share code, notes, and snippets.

@vb64
Created July 21, 2022 18:54
Show Gist options
  • Select an option

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

Select an option

Save vb64/eb2994f4e15e44707e259559088ed69d to your computer and use it in GitHub Desktop.
from django.urls import path
from django.http import HttpResponse
from django.contrib import admin
class Admin(admin.ModelAdmin):
"""Admin model with data download option."""
def get_urls(self):
"""Add own view to the standard admin views for data download.
Wrap it in a call of admin_site.admin_view so that it inherits all the rules
for working with the admin sections on security and user permissions.
"""
urls = super().get_urls()
urls.append(path('csv', self.admin_site.admin_view(self.csv_download)))
return urls
def csv_download(self, request):
"""View for data download."""
return HttpResponse(
'data for download should be here',
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