Skip to content
Snippets Groups Projects

Dev

Merged
Hamza Touizratrequested to merge
dev into master
3 files
+ 92
9
Compare changes
  • Side-by-side
  • Inline

Files

+ 19
1
@@ -2,7 +2,8 @@
from django.contrib import admin
from .models import Document, Address
from django.http import HttpResponse
import csv
# Register your models here.
@@ -16,6 +17,23 @@ class DocumentAdmin(admin.ModelAdmin):
# reorganize fields
fields = ('title', 'slug', 'content',)
actions = ["export_as_csv"]
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 = "Exporter au format CSV"
@admin.register(Address)
class AddressAdmin(admin.ModelAdmin):
Loading