14、Django Admin的“Action(动作)”中添加额外操作

如图红框增加操作

将以下代码添加到HeroAdmin类中

python 复制代码
actions = ["mark_immortal"]
def mark_immortal(self, request, queryset):
    queryset.update(is_immortal=True)

修改后完整代码如下:

python 复制代码
@admin.register(Hero)
class HeroAdmin(admin.ModelAdmin):
    list_display = ("name", "is_immortal", "category", "origin", "is_very_benevolent")
    list_filter = ("is_immortal", "category", "origin", IsVeryBenevolentFilter)
    def is_very_benevolent(self, obj):
        return obj.benevolence_factor > 75
    is_very_benevolent.boolean = True
    
    actions = ["mark_immortal"]
    
    def mark_immortal(self, request, queryset):
        queryset.update(is_immortal=True)

显示效果如下:

前

后,执行后状态改变

相关推荐
Csvn10 分钟前
Nginx 高可用网关:VIP 切换、proxy_protocol 与 SNI 分流实战
后端·nginx
Gopher_HBo12 分钟前
跟着一次导出,走完 runtime/pprof:从一张工单到一份文件
后端
2401_8685347828 分钟前
55 个热门网络面试问题及答案
网络·python
用户693717500138431 分钟前
大模型蒸馏到底在蒸什么?一篇文章搞懂知识蒸馏概念
前端·后端·github
aifannihao34 分钟前
ON CONFLICT 冲突后返回空:sqlite3 实测 get-or-create
数据库·python·postgresql·sqlite3·标准库
CRZZX41 分钟前
三引擎 Agent 方案设计与性能测试总结
后端·架构
用户0332126663671 小时前
使用 Python 合并 Excel 工作簿
python
bai2731 小时前
同一条 q=a+b:URLSearchParams 读成空格、decodeURIComponent 读成加号,11 条查询串打进裸 socket 的实测
javascript·后端
__underline__1 小时前
冷门Python知识点008篇:__int__、__index__与__float__,Python数值转换的真实回退顺序?
后端·python
学长毕设1 小时前
基于SpringBoot景区停车场预约管理系统-附源码
java·spring boot·后端