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)

显示效果如下:

后,执行后状态改变

相关推荐
云程笔记2 小时前
004.环境搭建基础篇:Python、CUDA、cuDNN、PyTorch/TensorFlow安装与版本兼容性踩坑
pytorch·python·tensorflow
知行合一。。。7 小时前
Python--04--数据容器(总结)
开发语言·python
架构师老Y7 小时前
008、容器化部署:Docker与Python应用打包
python·容器·架构
lifewange7 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
GreenTea8 小时前
一文搞懂Harness Engineering与Meta-Harness
前端·人工智能·后端
pluvium278 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
2401_827499998 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python
PD我是你的真爱粉8 小时前
MCP 协议详解:从架构、工作流到 Python 技术栈落地
开发语言·python·架构
ZhengEnCi8 小时前
P2G-Python字符串方法完全指南-split、join、strip、replace的Python编程利器
python
是小蟹呀^8 小时前
【总结】LangChain中工具的使用
python·langchain·agent·tool