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.

46 lines
1.2 KiB

  1. import csv
  2. from django.contrib import admin
  3. from django.http import HttpResponse
  4. from .models import Project, HonoraryCertificate, Library, IFG, Travel,\
  5. Email, BusinessCard, List, Literature
  6. def export_as_csv(self, request, queryset):
  7. meta = self.model._meta
  8. field_names = [field.name for field in meta.fields]
  9. response = HttpResponse(content_type='text/csv')
  10. response['Content-Disposition'] = 'attachment; filename={}.csv'.format(meta)
  11. writer = csv.writer(response)
  12. writer.writerow(field_names)
  13. for obj in queryset:
  14. row = writer.writerow([getattr(obj, field) for field in field_names])
  15. return response
  16. export_as_csv.short_description = "Export Selected"
  17. admin.site.add_action(export_as_csv)
  18. @admin.register(Project)
  19. class ProjectAdmin(admin.ModelAdmin):
  20. save_as = True
  21. readonly_fields = ('pid',)
  22. # action = ['export_as_csv']
  23. admin.site.register([
  24. HonoraryCertificate,
  25. Library,
  26. IFG,
  27. Travel,
  28. Email,
  29. BusinessCard,
  30. List,
  31. Literature,
  32. ])