如图红框增加操作
将以下代码添加到HeroAdmin类中
actions = ["mark_immortal"]
def mark_immortal(self, request, queryset):
queryset.update(is_immortal=True)
修改后完整代码如下:
@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)
显示效果如下:
前
后,执行后状态改变