Search code examples
djangodjango-admin

How can I display the column ID in the django-admin users table?


How can I show besides the columns username, email, first name, last name, staff user a 5th column with the users ID. I think I have to write some code into admin.py like:

class CustomUserAdmin(UserAdmin):
    readonly_fields = ('id',)

admin.site.register(CustomUserAdmin)

Solution

  • You should take advantage of ModelAdmin.list_display, docs here --> https://docs.djangoproject.com/en/2.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

    class CustomUserAdmin(UserAdmin):
        list_display = ('username', 'email', 'first_name', 'last_name', 'id')
        readonly_fields = ('id',)
    
    admin.site.register(User, CustomUserAdmin)
    

    In case you incur in an AlreadyRegistered error, you should add the following prior to your admin.site.register line

    admin.site.unregister(User)