Change Django Users List Display
Asked Answered
S

1

11

I am trying to change the Users view in Django Admin to show different fields (now it shows by default: username, email address, first name, last name, staff status) I have tried this in the admin.py file:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
# Register your models here.


UserAdmin.list_display = ['email', 'first_name', 'last_name', 'is_active', 'last_login']

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

However I cannot get my admin Users view to change. Is there anything else I need to do? I am using Django 2.0

Sandhurst answered 18/5, 2018 at 11:43 Comment(0)
O
15

Quite simply: define your own UserAdmin:

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

class MyUserAdmin(admin.ModelAdmin):
    list_display = ['email', 'first_name', 'last_name', 'is_active', 'last_login']

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
Obsequies answered 18/5, 2018 at 12:5 Comment(5)
Shouldn't MyUserAdmin inherit from django.contrib.auth.admin.UserAdmin?Kordofan
@dm295 no it "should" not - it can if you want to inherit other features from auth.admin.UserAdmin indeed, but it's not mandatory.Obsequies
I got this to work once I created a user with first/last name, and email. I was just creating a user with the username and password . thanksSandhurst
Would this go in a file called user.py or in admin.py?Grisby
@DavidRhoden Django expects your admin modules to be named 'admin.py' (that's how it discovers them).Obsequies

© 2022 - 2024 — McMap. All rights reserved.