import csv from django.contrib import admin from django.http import HttpResponse from .models import Account, Project, HonoraryCertificate, Library, IFG, Travel,\ Email, BusinessCard, List, Literature 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 = "Ausgewähltes zu CSV exportieren" admin.site.add_action(export_as_csv) @admin.register(Project) class ProjectAdmin(admin.ModelAdmin): save_as = True search_fields = ('name', 'pid','finance_id', 'realname', 'start', 'end', 'participants_estimated', 'participants_real', 'cost', 'status', 'end_quartal') list_display = ('name', 'pid','finance_id', 'realname', 'start', 'end', 'participants_estimated', 'participants_real', 'cost', 'status', 'end_quartal') # action = ['export_as_csv'] date_hierarchy = 'end' readonly_fields = ('end_quartal', 'project_of_year', 'pid', 'finance_id') @admin.register(BusinessCard) class BusinessCardAdmin(admin.ModelAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date', 'project') list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project') list_display_links = ('realname', 'service_id') # action = ['export_as_csv'] date_hierarchy = 'granted_date' readonly_fields = ['service_id'] class Media: js = ('dropdown/js/base.js',) @admin.register(Literature) class LiteratureAdmin(admin.ModelAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date') list_display_links = ('realname', 'service_id') date_hierarchy = 'granted_date' readonly_fields = ['service_id'] @admin.register(Account) class AccountAdmin(admin.ModelAdmin): save_as = True @admin.register(HonoraryCertificate) class HonoraryCertificateAdmin(admin.ModelAdmin): save_as = True search_fields = ['realname', 'granted'] list_display = ('realname', 'granted','project') date_hierarchy = 'granted_date' @admin.register(Library) class LibraryAdmin(admin.ModelAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date') list_display_links = ('realname', 'service_id') date_hierarchy = 'granted_date' readonly_fields = ['service_id'] @admin.register(IFG) class IFGAdmin(admin.ModelAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date') list_display_links = ('realname', 'service_id') date_hierarchy = 'granted_date' readonly_fields = ['service_id'] @admin.register(Travel) class TravelAdmin(admin.ModelAdmin): save_as = True search_fields = ['realname', 'granted_date'] list_display = ('realname', 'granted', 'granted_date', 'project_end', 'project', 'project_end_quartal') list_display_links = ('realname', 'project') date_hierarchy = 'project_end' readonly_fields = ('project_end_quartal', 'project_end') autocomplete_fields = ['project'] @admin.register(Email) class EmailAdmin(admin.ModelAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date') list_display_links = ('realname', 'service_id') date_hierarchy = 'granted_date' radio_fields = {'adult': admin.VERTICAL} readonly_fields = ['service_id'] class Media: js = ('dropdown/js/base.js',) @admin.register(List) class ListAdmin(admin.ModelAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date') list_display_links = ('realname', 'service_id') date_hierarchy = 'granted_date' readonly_fields = ['service_id'] # commented out because of the individual registering to control displays in admin panel #admin.site.register([ # Account, # HonoraryCertificate, # Library, # IFG, # Travel, # Email, # List, # ])