list_display - 布尔图标不适用于BooleanField

时间:2012-11-20 00:19:39

标签: django django-models django-admin

简单的情况,型号:

class Manufacturer(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, null=True)
    active = models.BooleanField()
    ...

和ModelAdmin:

class ManufacturerAdmin(admin.ModelAdmin):
    search_fields = ['name']
    list_filter = ['assortment_type', 'active']
    ordering = ('priority',)
    prepopulated_fields = {'slug':('name',),}
    list_display = ('name', 'active')

仍然在管理员活动列中显示为True / False而不是图标。我可以创建方法并设置Boolean = True属性但是我失去了对列的排序能力。很明显,我在这里遗漏了一些东西。

1 个答案:

答案 0 :(得分:3)

创建自定义方法,然后将admin_order_field设置为字段名称,将boolean设置为True

def my_function(self):
    # your stuff goes here
my_function.boolean = True
my_function.admin_order_field = 'active'

这应该使它可以排序(doc