django import_export 按用户类型显示导入导出按钮

2023-02-18 15:56:53 浏览数 (2)

代码语言:javascript复制
@admin.register(InvestProject)
# class InvestProjectAdmin(admin.ModelAdmin):
class InvestProjectAdmin(ImportExportActionModelAdmin, ImportExportModelAdmin):
    """
    投资项目
    """

    list_display = list_display_links = 
        ["name", "code", "is_gold", "scale", "create_date", "fund_manager", "fund_manager_work_duration",
         "return_rate_3y", "shape_rate", "max_return_rate", "volatility", "is_good", "time_update",
         "to_project_detail"]
    # "today_est_point", "yesterday_rel_point",
    #          "up_or_down_percent",

    search_fields = ("code", "name", "fund_manager__name",)
    add_form_template = "change_form_invest_project.html"
    change_form_template = "change_form_invest_project.html"
    list_filter = ["is_gold", "is_good", "is_have", "fund_company", "kind", ]
    resource_class = InvestProjectResource

    def changelist_view(self, request, extra_context=None):
        if extra_context is None:
            extra_context = {}

        # E:py_virtualenvjoyooLibsite-packagesimport_exportadmin.py 340
        extra_context['has_import_permission'] = request.user.is_superuser
        # E:py_virtualenvjoyooLibsite-packagesimport_exportadmin.py 506
        extra_context['has_export_permission'] = request.user.is_superuser
        print(extra_context)

        # 默认是是按照用户对表的权限显示导出导入按钮
        # 按照用户账户的类型显示导出导入按钮 ..Libsite-packagesimport_exporttemplatesadminimport_export
        if request.user.is_superuser:
            # 显示导入导出
            self.change_list_template = "admin/import_export/change_list_import_export.html"
        else:
            # 不显示导入导出
            # self.change_list_template = "admin/import_export/change_list.html"
            # 仅有导入按钮
            # self.change_list_template = "admin/import_export/change_list_import.html"
            # 仅有导出按钮
            self.change_list_template = "admin/import_export/change_list_export.html"
        return super().changelist_view(request, extra_context)

    def is_good_true(self, request, queryset):
        queryset.update(is_good=True)

    def is_good_false(self, request, queryset):
        queryset.update(is_good=False)

    is_good_true.short_description = "投资项目 优质 设置为 是"  # 新字段的显示的名称
    is_good_false.short_description = "投资项目 优质 设置为 否"  # 新字段的显示的名称

    actions = [is_good_true, is_good_false]

0 人点赞