创建一个自定义按钮,然后写上方法即可,如下
from import_export import resources
from .models import MyModel
class MyModelResource(resources.ModelResource):
class Meta:
model = MyModel
class MyModelAdmin(ImportExportModelAdmin):
# 增加自定义按钮
actions = ["report_choice"]
# 开始写方法
def report_choice(self, request, queryset):
add_weight_resource = MyModelResource()
dataset = add_weight_resource.export(queryset)
response = HttpResponse(dataset.xls, content_type="application/vnd.ms-excel")
response["Content-Disposition"] = 'attachment; filename="persons.xls"'
response["Access-Control-Expose-Headers"] = "Content-Disposition"
return response
report_choice.short_description = "导出选中"
report_choice.icon = "fas fa-file-excel"
report_choice.type = "warning"