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

4 Answers 4

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)
12
  • 10
    We can also extends the UserAdmin instead of dynamically modifying it !
    – Natim
    Feb 16, 2010 at 4:49
  • 6
    To clarify, this should be added to your site's top-level admin.py file. Mar 23, 2011 at 11:39
  • 1
    can I ask what you mean by top-level?
    – tani-rokk
    Dec 5, 2014 at 14:16
  • 1
    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.)
    – Natim
    Dec 12, 2014 at 6:53
  • 2
    Maybe you are missing from django.contrib import admin ?
    – Natim
    Feb 16, 2016 at 16:48
23

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)
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']
    – jdm
    Dec 12, 2016 at 10:01
  • 2
    shouldn't that be CustomUserAdmin.list_display = .. rather than UserAdmin.list_display = ? Dec 20, 2018 at 14:30
  • @AmichaiSchreiber I think both works, but I agree that CustomUserAdmin.list_display = is the cleaner solution Oct 14, 2019 at 18:30
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

1
  • Or you can extend the list_display with additional fields (using Django4): UserAdmin.list_display = list(UserAdmin.list_display) + ['is_active', 'date_joined'] Nov 30, 2022 at 16:10
-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)
1
  • 5
    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.
    – Carl Meyer
    Feb 16, 2010 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.