25

How do you override the admin model for Users? I thought this would work but it doesn't?

class UserAdmin(admin.ModelAdmin):
    list_display = ('email', 'first_name', 'last_name')
    list_filter = ('is_staff', 'is_superuser')

admin.site.register(User, UserAdmin)

I'm not looking to override the template, just change the displayed fields & ordering.

Solutions please?

3 Answers 3

53

You have to unregister User first:

class UserAdmin(admin.ModelAdmin):
    list_display = ('email', 'first_name', 'last_name')
    list_filter = ('is_staff', 'is_superuser')


admin.site.unregister(User)
admin.site.register(User, UserAdmin)

Maybe this question is also interesting for you: Customizing an Admin form in Django while also using autodiscover

3
  • @second: In a file called admin.py in the particular application., see also docs.djangoproject.com/en/1.2/ref/contrib/admin Aug 20, 2010 at 7:36
  • 5
    extend ModelAdmin doesnt work for me. I tried extending from UserAdmin then my CustomUserAdmin works Jan 26, 2017 at 21:40
  • 1
    You must extend the UserAdmin otherwise user creation and updating will not funtion properly.
    – SEDaradji
    Oct 18, 2020 at 20:29
12

Update tested with Django 3+

So you don't lose data like password encryption and the form itself, perform the import below

from django.contrib.auth.admin import UserAdmin

Define an AdminCustom class as the example and customize with the options you want, overriding the default.

class UserAdminCustom(UserAdmin):
   list_display = ('email', 'first_name', 'last_name', 'is_staff', 'is_superuser')
   list_filter = ('is_staff', 'is_superuser')
   search_fields = ('username', )

And in the end, follow the example mentioned

admin.site.unregister(User)
admin.site.register(User, UserAdminCustom)
1
  • 1
    This should be the correct answer, however if you already have AUTH_USER_MODEL set to a custom user model, than "admin.site.unregister(User)" should be omitted.
    – SEDaradji
    Oct 18, 2020 at 20:35
4

As pointed by @haifeng-zhang in the comments, it is useful to extend the default UserAdmin instead.

The official documentation about this can be found here: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model

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


# Define a new User admin
class UserAdmin(BaseUserAdmin):
    list_display = ('email', 'first_name', 'last_name')
    list_filter = ('is_staff', 'is_superuser')

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

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.