8

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?

3 Answers 3

6

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.

1
  • 2
    I realize it's not a huge deal in the scheme of things, but it seems extreme for such a small thing (I'm not blaming you obviously, it's just disappointing ;-) Thanks for answering! Commented Sep 7, 2012 at 17:16
5

admin.py:

class MyClassAdmin(admin.ModelAdmin):

    class Media:
        css = {
            'all': ('fancy.css')
        }

fancy.css:

.column-foo {
    width: 20px;
}

where "foo" is a field name.

Source: https://docs.djangoproject.com/en/3.0/topics/forms/media/

0

Modifying the css is overkill just to force Django to do something simple like not to squashing datetime fields by breaking on whitespace. If you want to just implements a minimum column width to ensure the contents of a cell do not break into multiple lines you can so something really simple like this:

from django.utils.html import format_html

@admin.register(MyClass)
class MyClassAdmin(admin.ModelAdmin):

    def changed_time(self, obj):
        return format_html(obj.changed_on.strftime("%b %d. %Y %H:%M:%S.%f %Z").replace(' ', ' '))
    changed_time.short_desription = 'Changed On'

    list_display = ('id', 'user_id', 'changed_time)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.