new action save as csv in admin

This commit is contained in:
Benni Bärmann 2020-11-24 13:44:07 +01:00
parent 3132bc9647
commit 18669a4116
2 changed files with 40 additions and 1 deletions

View File

@ -6,7 +6,7 @@ purpose: gather data from intern(WMDE) and extern(volunteers) forms to create a
ln -sr foerderbarometer/settings_development.py foerderbarometer/settings.py ln -sr foerderbarometer/settings_development.py foerderbarometer/settings.py
build database with build the database with
python3 manage.py migrate python3 manage.py migrate
@ -18,12 +18,25 @@ run the development server with
python3 manage.py runserver python3 manage.py runserver
you can run some very basic tests with
python3 manage.py test
access via access via
http://localhost/ http://localhost/
http://localhost/intern/ (login required) http://localhost/intern/ (login required)
http://localhost/admin/ (login reqiured) http://localhost/admin/ (login reqiured)
## additional admin functionality
The admin page is the standard admin page delivered by django but with two additional functionalities:
- There is a new action "export to csv" with which you can export all Selected
entries to a csv file
- There is a new button in the bottom of every Project to "save as new"
## versions used in development ## versions used in development
python 3.8.2 python 3.8.2

View File

@ -1,12 +1,38 @@
import csv
from django.contrib import admin from django.contrib import admin
from django.http import HttpResponse
from .models import Project, HonoraryCertificate, Library, IFG, Travel,\ from .models import Project, HonoraryCertificate, Library, IFG, Travel,\
Email, BusinessCard, List Email, BusinessCard, List
def export_as_csv(self, request, queryset):
meta = self.model._meta
field_names = [field.name for field in meta.fields]
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:
row = writer.writerow([getattr(obj, field) for field in field_names])
return response
export_as_csv.short_description = "Export Selected"
admin.site.add_action(export_as_csv)
@admin.register(Project) @admin.register(Project)
class ProjectAdmin(admin.ModelAdmin): class ProjectAdmin(admin.ModelAdmin):
save_as = True save_as = True
readonly_fields = ('pid',) readonly_fields = ('pid',)
# action = ['export_as_csv']
admin.site.register([ admin.site.register([
HonoraryCertificate, HonoraryCertificate,