gpt4 book ai didi

python - 我自己的方法用于 list_display 和值作为 bool 图标

转载 作者:太空狗 更新时间:2023-10-29 20:54:36 24 4
gpt4 key购买 nike

我自己编写了用于 list_display(管理类)的方法,如下所示:

class MyClassAdmin(admin.ModelAdmin):

list_display = ('my_own_method')

def my_own_method(self, obj):
if [condition]:
return True
else:
return False

但是这个值在列表中显示为文本(True 或 False),而不是像这样的默认 django bool 图标: enter image description here

我应该怎么做才能改变它?

最佳答案

将您的代码更改为以下内容:

class MyClassAdmin(admin.ModelAdmin):

list_display = ('my_own_method')

def my_own_method(self, obj):
if [condition]:
return True
else:
return False
my_own_method.boolean = True

可在 documentation 中找到在 list_display 上:

If the string given is a method of the model, ModelAdmin or a callable that returns True or False Django will display a pretty "on" or "off" icon if you give the method a boolean attribute whose value is True.

关于python - 我自己的方法用于 list_display 和值作为 bool 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11393060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com