You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

47 lines
1.2 KiB

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 = "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([
Account,
HonoraryCertificate,
Library,
IFG,
Travel,
Email,
BusinessCard,
List,
Literature,
])