Developer 2024 🎇限时优惠进行中,现在购买立即享受

现在购买

Django-admin如何基于simpleui导出选中数据?

avatarAbraverman
12月22日922次阅读

创建一个自定义按钮,然后写上方法即可,如下


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"
发布评论
登录后发表内容
5个评论