简体   繁体   中英

Show Django Model @property as a bool in model admin

I have a model with a property , It returns boolean, I want to show it as icon in django model admin.

models.py

class Foo(models.Model):
    bar = models.TextField("Title", null=True, blank=True)

    @property
    def is_new_bar(self):
        return bar == 'NEW'

admin.py

class FooAdmin(admin.ModelAdmin):
    list_display = ('bar', 'is_new_bar') # is_new_bar is shown as True/False text, I want this as bool icon of django.

布尔图标

You can add method to your modeladmin that will return property value and set that it will return boolean:

class FooAdmin(admin.ModelAdmin):
     list_display = ('bar', 'get_is_new_bar') 

     def get_is_new_bar(self, obj):
        return obj.is_new_bar
     get_is_new_bar.boolean = True

Try this property use.

def is_new_bar(self):
    return bar == 'NEW'
is_new_bar.boolean = True
is_new_bar = property(is_new_bar)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM