-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from arkavidia-hmif/bugfix-and-exportcsv
Bug fixes, add export models as csv from admin panel
- Loading branch information
Showing
8 changed files
with
86 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
File renamed without changes.
File renamed without changes.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import csv | ||
from operator import attrgetter | ||
from django.http import HttpResponse | ||
|
||
DEEP_SEPARATOR = '__' | ||
|
||
|
||
class ExportCsvMixin: | ||
def export_as_csv(self, request, queryset): | ||
|
||
meta = self.model._meta | ||
if hasattr(self, 'csv_fields'): | ||
field_names = [x.replace(DEEP_SEPARATOR, '.') for x in self.csv_fields] | ||
else: | ||
field_names = self.list_display | ||
|
||
response = HttpResponse(content_type='text/csv') | ||
response['Content-Disposition'] = 'attachment; filename={}.csv'.format(meta) | ||
writer = csv.writer(response) | ||
|
||
writer.writerow(field_names) | ||
for obj in queryset: | ||
writer.writerow([attrgetter(field)(obj) for field in field_names]) | ||
|
||
return response | ||
|
||
export_as_csv.short_description = 'Export selected as csv' |