回到顶部

Contents

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

@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_virtualenv\joyoo\Lib\site-packages\import_export\admin.py 340
        extra_context['has_import_permission'] = request.user.is_superuser
        # E:\py_virtualenv\joyoo\Lib\site-packages\import_export\admin.py 506
        extra_context['has_export_permission'] = request.user.is_superuser
        print(extra_context)

        # 默认是是按照用户对表的权限显示导出导入按钮
        # 按照用户账户的类型显示导出导入按钮 ..\Lib\site-packages\import_export\templates\admin\import_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]

 

^_^
请喝咖啡 ×

本作品由 卓越笔记 采用 知识共享署名 - 非商业性使用 - 相同方式共享 4.0 国际许可协议 进行许可
前一篇: docker 常用命令
下一篇: centos7 systemctl 添加 uwsgi(开机启动)
captcha