django 自定义操作名称根据模型名称更改

问题描述

我创建了一个自定义管理操作。我在多个列表页面中使用该操作。

在我的 admin.py 文件

def custom_action(request,modelAdmin,queryset):
    #do something here
    return response

custom_action.short_description = 'My custom action for X'


class MyXAdmin(admin.ModelAdmin):
      actions = [custom_action]

class MyYAdmin(admin.ModelAdmin):
      actions = [custom_action]

在我的 X 和 Y 列表页面中,它都显示“我对 x 的自定义操作”。有什么方法可以更改 Y 列表页面的“X”=>“Y”。

如何在不为每个模型编写自定义方法的情况下实现在 djagno 中重命名单个列表页面的 custom_action 名称

解决方法

试试这个:

class MyXAdmin(admin.ModelAdmin):
      actions = [custom_action]
      custom_action.short_description = 'My custom action for X'

class MyYAdmin(admin.ModelAdmin):
      actions = [custom_action]
      custom_action.short_description = 'My custom action for Y'