def export_csv(modeladmin, request, queryset):
import csv
from django.utils.encoding import smart_str
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=elehphant_sightings.csv'
writer = csv.writer(response, csv.excel)
response.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)
writer.writerow([
smart_str(u"ID"),
smart_str(u"Reported At"),
smart_str(u"Latitude"),
smart_str(u"Longitude"),
smart_str(u"Message"),
smart_str(u"Informer"),
])
for obj in queryset:
writer.writerow([
smart_str(obj.pk),
smart_str(str(obj.created_at)),
smart_str(obj.location),
smart_str(obj.message),
smart_str(obj.informer.name),
])
return response
评论列表
文章目录