[Python] Provide short_description for model field in Django Admin

Discussão em 'Python' iniciado por Stack, Abril 6, 2025.

  1. Stack

    Stack Membro Participativo

    Sometimes I want to display many boolean fields in the list_display view in Django Admin. Unfortunately these fields come with a long verbose_name which gives very ugly and large columns.

    I know that you can provide a short_description attribute to callables for providing column names in the list_display view in Django Admin. So I reverted to doing this:

    class MyModelAdmin(admin.ModelAdmin):
    list_display = ['id', 'field1', 'field2', 'short_boolfield3', 'short_boolfield4']

    def short_boolfield3(self, obj):
    return obj.boolfield3
    short_boolfield3.short_description = 'fld3'
    short_boolfield3.boolean = True

    def short_boolfield4(self, obj):
    return obj.boolfield4
    short_boolfield4.short_description = 'fld4'
    short_boolfield4.boolean = True


    Which works, but is definitely against DRY, especially when you have many boolean fields.

    Is there a short_description equivalent for Model fields?

    Continue reading...

Compartilhe esta Página