Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin and column width through list_display

I read in Django >= 1.6 docs:

"The field names in list_display will also appear as CSS classes in the HTML output, in the form of column- on each element. This can be used to set column widths in a CSS file."

OK. But, how?

class bollaAdmin(admin.ModelAdmin):
  ordering = ['num']
  list_display = ('num|width=15', 'Vendemmia','Cultivar', 'Provenienza' , 'netto', 'grado','montegradi')
like image 754
bleish Avatar asked Jul 18 '26 11:07

bleish


2 Answers

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;
}
like image 54
Dominic Rodger Avatar answered Jul 22 '26 09:07

Dominic Rodger


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')
like image 30
monkut Avatar answered Jul 22 '26 09:07

monkut