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
| import io | |
| import csv | |
| def queryset2csv(qset): | |
| """Converts a Django queryset to the content of a csv file.""" | |
| out = io.StringIO() | |
| writer = csv.writer(out, delimiter=';', lineterminator='\n') | |
| for row in qset: | |
| writer.writerow([row.field1, row.field2, ...]) |
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.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, |
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
| {% extends "admin/change_list.html" %} | |
| {% block content %} | |
| <div><a href="csv/?{{ request.GET.urlencode }}">Data download</a></div> | |
| {{ block.super }} | |
| {% endblock %} |
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. |