How to customize the auth.User Admin page in Django CRUD?
Asked Answered
O

4

52

I just want to add the subscription date in the User list in the Django CRUD Administration site. How can I do that ?

Thank you for your help

Otology answered 16/2, 2010 at 3:59 Comment(0)
O
87

I finally did like this in my admin.py file :

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

UserAdmin.list_display = ('email', 'first_name', 'last_name', 'is_active', 'date_joined', 'is_staff')

admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Otology answered 16/2, 2010 at 4:45 Comment(12)
We can also extends the UserAdmin instead of dynamically modifying it !Otology
To clarify, this should be added to your site's top-level admin.py file.Labelle
can I ask what you mean by top-level?Uphold
It means that you should create an admin.py file at the root of your Django project (on the same level as your wsgi.py or settings.py files.)Otology
It doesn't seem like you need to unregister and reregisterTramline
According to @Carl Meyer on the other answer you need to unregister. How would you do without unregistering?Lucius
using your code above I do get import error as admin is not foundHomebred
I think the code organisation may have changed in the last versions of Django.Otology
No it didn't: github.com/django/django/blob/master/django/contrib/auth/…Otology
Maybe you are missing from django.contrib import admin ?Otology
This is also asked as "how to make a project admin.py" that might be helpful to people who land here. #24128886Ameliorate
@JamieForrest In Django 2.1 top-level admin.py file doesn't work. I put it to my own App:(accounts.admin.py) then it works, Don't know why but anyway it works. BTW: There is a full example in docs.djangoproject.com/en/2.1/topics/auth/customizingTypeset
B
24

Another way to do this is extending the UserAdmin class.

You can also create a function to put on list_display

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

class CustomUserAdmin(UserAdmin):
    def __init__(self, *args, **kwargs):
        super(UserAdmin,self).__init__(*args, **kwargs)
        UserAdmin.list_display = list(UserAdmin.list_display) + ['date_joined', 'some_function']

    # Function to count objects of each user from another Model (where user is FK)
    def some_function(self, obj):
        return obj.another_model_set.count()


admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
Binnacle answered 24/9, 2015 at 16:54 Comment(3)
Can I also do the following (without __init__), or is there a problem with it? class CustomUserAdmin(UserAdmin): list_display = list(UserAdmin.list_display) + ['date_joined']Donyadoodad
shouldn't that be CustomUserAdmin.list_display = .. rather than UserAdmin.list_display = ?Reynold
@AmichaiSchreiber I think both works, but I agree that CustomUserAdmin.list_display = is the cleaner solutionExpertise
B
1

In admin.py

Import UserAdmin

from django.contrib.auth.admin import UserAdmin

Put which fields you need:

UserAdmin.list_display = ('email','is_active')  # Put what you need

Thats all! It works with Django3

Bazan answered 19/1, 2021 at 23:10 Comment(1)
Or you can extend the list_display with additional fields (using Django4): UserAdmin.list_display = list(UserAdmin.list_display) + ['is_active', 'date_joined']Impatient
E
-3

Assuming that your user class is User and your subscription date field is subscription_date, this is what you need to add on your admin.py

class UserAdmin(admin.ModelAdmin):
    list_display = ('subscription_date',)

admin.site.register(User, UserAdmin)
Emigrant answered 16/2, 2010 at 4:17 Comment(1)
This should inherit from the built-in UserAdmin, otherwise you lose all the rest of the customizations. And you have to unregister the built-in registration too; Natim's answer has the right code.Vichy

© 2022 - 2024 — McMap. All rights reserved.