new action save as csv in admin

Benni Bärmann 3 years ago
parent 3132bc9647
commit 18669a4116

@ -6,7 +6,7 @@ purpose: gather data from intern(WMDE) and extern(volunteers) forms to create a
ln -sr foerderbarometer/ foerderbarometer/
build database with
build the database with
python3 migrate
@ -18,12 +18,25 @@ run the development server with
python3 runserver
you can run some very basic tests with
python3 test
access via
http://localhost/intern/ (login required)
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
python 3.8.2

@ -1,12 +1,38 @@
import csv
from django.contrib import admin
from django.http import HttpResponse
from .models import Project, HonoraryCertificate, Library, IFG, Travel,\
Email, BusinessCard, List
def export_as_csv(self, request, queryset):
meta = self.model._meta
field_names = [ for field in meta.fields]
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename={}.csv'.format(meta)
writer = csv.writer(response)
for obj in queryset:
row = writer.writerow([getattr(obj, field) for field in field_names])
return response
export_as_csv.short_description = "Export Selected"
class ProjectAdmin(admin.ModelAdmin):
save_as = True
readonly_fields = ('pid',)
# action = ['export_as_csv'][