John Smith John Smith - 3 months ago 24
Python Question

Ordering users by date created in django admin panel

How do you order users in the django admin panel so that upon display they are ordered by date created? Currently they are listed in alphabetical order

I know that I can import the

User
model via:
from django.contrib.auth.models import User


How do I go about doing this?

Answer

To change the default ordering of users in admin panel you can subclass the default UserAdmin class. In your applications's admin.py:

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

class MyUserAdmin(UserAdmin):
    # override the default sort column
    ordering = ('date_joined', )
    # if you want the date they joined or other columns displayed in the list,
    # override list_display too
    list_display = ('username', 'email', 'date_joined', 'first_name', 'last_name', 'is_staff')

# finally replace the default UserAdmin with yours
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

For more information refer to the documentation.

Comments