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

Natim
  • 17,274
  • 23
  • 92
  • 150

4 Answers4

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)
Natim
  • 17,274
  • 23
  • 92
  • 150
  • 10
    We can also extends the UserAdmin instead of dynamically modifying it ! – Natim Feb 16 '10 at 04:49
  • 6
    To clarify, this should be added to your site's top-level admin.py file. – Jamie Forrest Mar 23 '11 at 11:39
  • 1
    can I ask what you mean by top-level? – tani-rokk Dec 05 '14 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 '14 at 06:53
  • It doesn't seem like you need to unregister and reregister – Lee May 07 '15 at 08:05
  • 1
    According to @Carl Meyer on the other answer you need to unregister. How would you do without unregistering? – giancarloap May 21 '15 at 18:45
  • using your code above I do get import error as admin is not found – patroqueeet Feb 16 '16 at 09:51
  • I think the code organisation may have changed in the last versions of Django. – Natim Feb 16 '16 at 13:35
  • No it didn't: https://github.com/django/django/blob/master/django/contrib/auth/admin.py – Natim Feb 16 '16 at 16:47
  • 2
    Maybe you are missing ``from django.contrib import admin`` ? – Natim Feb 16 '16 at 16:48
  • This is also asked as "how to make a project admin.py" that might be helpful to people who land here. https://stackoverflow.com/questions/24128386/admin-py-for-project-not-app/45313274#45313274 – Patrick Jul 25 '17 at 21:19
  • 1
    @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 https://docs.djangoproject.com/en/2.1/topics/auth/customizing/ – C.K. Sep 19 '18 at 23:46
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)
Fernando Freitas Alves
  • 3,709
  • 3
  • 26
  • 45
  • 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 '16 at 10:01
  • 2
    shouldn't that be `CustomUserAdmin.list_display = ` .. rather than `UserAdmin.list_display = ` ? – Amichai Schreiber Dec 20 '18 at 14:30
  • @AmichaiSchreiber I think both works, but I agree that `CustomUserAdmin.list_display = `is the cleaner solution – Erik Kalkoken Oct 14 '19 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

devugur
  • 1,339
  • 1
  • 19
  • 25
  • Or you can extend the `list_display` with additional fields (using Django4): `UserAdmin.list_display = list(UserAdmin.list_display) + ['is_active', 'date_joined']` – Domenico Spidy Tamburro Nov 30 '22 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)
Joshua Partogi
  • 16,167
  • 14
  • 53
  • 75
  • 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 '10 at 19:00