admin.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:elephant-sms-ews 作者: stultus 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号