Created
July 21, 2022 18:54
-
-
Save vb64/eb2994f4e15e44707e259559088ed69d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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