Django Admin Cookbook-13如何从Django Admin后台中导出CSV
返回目录
2.如何从Django Admin后台中导出CSV?
假设你需要在Admin后台中,增加从导出Hero和导出Villain数据的功能。有许多Django第三方插件可以实现此操作,但是要自己实现也非常容易。你需要再HeroAdmin和VillanAdmin两个管理模型中添一个额外动作。
Admin后台操作方法的签名是固定的,def admin_action(modeladmin, request, queryset):
,你可以直接作为方法添加到你的管理模型中。
class SomeModelAdmin(admin.ModelAdmin):
def admin_action(self, request, queryset):
要将csv导出功能添加到HeroAdmin管理模型中,你可以修改如下:
actions = ["export_as_csv"]
def export_as_csv(self, request, queryset):
pass
export_as_csv.short_description = "Export Selected"
这将为后台添加一个名为export selected的额外操作,效果如下:
然后,将export_as_csv方法完善为:
import csv
from django.http import HttpResponse
...
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方法并不依赖Hero模型,因此可以将该方法封装到一个mixin类中。
将代码修改如下:
class ExportCsvMixin:
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.register(Hero)
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin):
list_display = ("name", "is_immortal", "category", "origin", "is_very_benevolent")
list_filter = ("is_immortal", "category", "origin", IsVeryBenevolentFilter)
actions = ["export_as_csv"]
...
@admin.register(Villain)
class VillainAdmin(admin.ModelAdmin, ExportCsvMixin):
list_display = ("name", "category", "origin")
actions = ["export_as_csv"]
你可以在管理模型中,通过继承ExportCsvMixin来为该模型添加导出CSV功能。