[Django]-Django admin and column width through list_display

5👍

Here’s a snippet of HTML for the column containing the attribute headline from one of my admins:

<th scope="col" class="sortable column-headline">
   <div class="text"><a href="?o=2.4.-5">Headline</a></div>
   <div class="clear"></div>
</th>

You could set the width of that in CSS like this:

th.column-headline {
  width: 10000000px;
}

1👍

Here’s the lazy person’s method to extend a column width in the django admin without doing a css override.

from django.utils.html import format_html

class MyModelAdmin(admin.ModelAdmin):
    ...
    def get_column_extended_field(self, obj):
        result = ''
        field_value = obj.field_value
        if field_value:
            spaces = '&nbsp;' * 75
            result = format_html('{result}<br/>' + spaces, result=field_value)
        return result
    get_column_extended_field.short_description = _('Extended Field')
👤monkut

Leave a comment