[Django]-Custom value for list_display item on Django admin

43👍

list_display can accept a callable, so you can do this:

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('get_sub_title',)

    def get_sub_title(self, obj):
        if obj.sub_title:
            return obj.sub_title
        else:
            return 'Not Available'

    get_sub_title.short_description = 'Subtitle'

The docs provide several other options for providing a callable.

13👍

It would be better if you use empty_value_display

empty_value_display
This attribute overrides the default display value for record’s fields that are empty (None, empty string, etc.). The default value is – (a dash).

class AuthorAdmin(admin.ModelAdmin):
    list_display = ('sub_title',)
    sub_title.empty_value_display = 'Not Available'

This is for django >= 1.9

Leave a comment