且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

添加list_filter时,如何修改/设置django modeladmin更改列表中的列宽?

更新时间:2023-09-26 20:00:58

你可能需要参考: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-admin-template



基本上,change_list.html需要被覆盖。



你可以这样做:

  templates / 
admin /
app /
change_list.html

您可以获得copy_list.html的副本从 django / contrib / admin / templates / admin /



,并以css的方式更新你的愿望


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:

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?

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?

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.