Tweaking Django auth admin

So, I wanted one of those horizontal multi-selects with “available” and “chosen” boxes to associate users with groups in the Django auth admin UI.  Turns out it all I had to do was import the UserAdmin class from django.contrib.auth.admin and override the filter_horizontal attribute:

from django.contrib.auth.admin import UserAdmin

UserAdmin.filter_horizontal = ('user_permissions', 'groups')

Since importing the UserAdmin class runs the admin module from django.contrib.auth, the UserAdmin and GroupAdmin classes are registered for the admin site.  All I have to do then is import my custom admin module in my URLconf instead of the one from django.contrib.auth to make sure my customizations are applied in my admin site.

Update 1/5/11

As discovered by a commenter, there are some admin attributes which can’t be overridden as simply as shown above.  However, you can first unregister the model with the admin site (this method is not publicly documented), override, then re-register the model:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

class CustomUserAdmin(UserAdmin):
    filter_horizontal = ('user_permissions', 'groups')
    save_on_top = True
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'last_login'), CustomUserAdmin)


  1. #1 by bjubb on October 2, 2009 - 9:49 am

    Exactly how did you import your admin instead of the django.contrib.auth, yet also allow the autodiscover routine to run? Thanks – this tip should help me out nicely if I can get the import to work.

  2. #2 by David Chandek-Stark on October 2, 2009 - 8:26 pm

    I don’t use autodiscover because I want more control over what gets added to the admin UI, so I’m not sure how this recipe plays in that scenario. You might simply be able to add the lines I’ve given before or after the autodiscover call (or import the module that contains them, which is what I do). As I said, importing the UserAdmin class from django.contrib.auth.admin effectively registers the admin classes from that module. Does autodiscover blow up if it encounters admin classes that are already registered?

  3. #3 by bjubb on October 5, 2009 - 9:44 am

    i dont use autodiscover and use this instead in attempt to change which items are shown on the Auth->Users page:

    from django.contrib.auth.admin import UserAdmin


    The User/Group classes register fine, its just that my changes to the list_display dont work.

  4. #4 by bjubb on October 5, 2009 - 9:46 am

    Sorry – here’s the real line of code:
    UserAdmin.list_display = (‘username’, ’email’, ‘first_name’, ‘last_name’, ‘is_staff’, ‘last_login’)

    Trying to add the last login to the page….it works if i just edit the actual django admin file , but obviously i liked your method better.

  5. #5 by David Chandek-Stark on January 5, 2011 - 2:02 pm

    See the 1/5/11 update, which shows how you can do this.

  6. #6 by Ryan Blunden on May 29, 2011 - 9:06 pm

    Thanks for this write up, updates marked 1/5/11 worked perfectly for me.