pythondjangodjango-admindjango-admin-filters

How to fix/set column width in a django modeladmin change list table when a list_filter is added?


I'm working on improving the admin.py in a django project, and while I'm not totally jazzed about how the table was coming out with three fields in the list_diplay, at least it's better than just getting a default object list with one column spanning the whole page...

Anyway, what I'm asking is why if this:

class FieldAdmin(admin.ModelAdmin):
    list_display = ('name', 'label', 'standard',  )

looks like this:

without list_filter

When I add a list_filter, like this:

class FieldAdmin(admin.ModelAdmin):
    list_display = ('name', 'label', 'standard',  )
    list_filter = ['standard',]

Why does it look like this?

Imgur

Is there a way to get the columns to re-grow to fill the width like it was prior to adding the filter? I've been reading the docs and googling, but it doesn't seem built in? The project I'm working on is currently using django 1,2,3,final.

FWIW, the css that causes this is here:

.change-list .filtered table, .change-list .filtered .paginator, 
.filtered #toolbar, .filtered div.xfull {
    margin-right: 160px !important;
    width: auto !important;
}

disabling the width style specification fixes it, but I'd rather do things the django way if there is one - I was hoping maybe there's a way to customize the filter view from the FieldAdmin class?


Solution

  • You may want to refer to this: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-admin-template

    basically, the change_list.html needs to be overridden .

    you can do it this way:

    templates/
      admin/
        app/
          change_list.html
    

    you can obtain a copy of change_list.html from django/contrib/admin/templates/admin/

    and update the css the way you desire.