From 18669a4116e75e7b38e7d4ab28e986b37dd068a8 Mon Sep 17 00:00:00 2001 From: Benni Baermann Date: Tue, 24 Nov 2020 13:44:07 +0100 Subject: [PATCH] new action save as csv in admin --- README.md | 15 ++++++++++++++- input/admin.py | 26 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4d0b18c..e483bf6 100644 --- a/README.md +++ b/README.md @@ -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 -build database with +build the database with python3 manage.py migrate @@ -18,12 +18,25 @@ run the development server with python3 manage.py runserver +you can run some very basic tests with + + python3 manage.py test + access via http://localhost/ 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 diff --git a/input/admin.py b/input/admin.py index f3e8382..0e32935 100644 --- a/input/admin.py +++ b/input/admin.py @@ -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 = [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) class ProjectAdmin(admin.ModelAdmin): save_as = True readonly_fields = ('pid',) + # action = ['export_as_csv'] + + admin.site.register([ HonoraryCertificate,