Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...