Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering users by date created in django admin panel

Tags:

python

django

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?

like image 340
John Smith Avatar asked Aug 10 '16 16:08

John Smith


People also ask

How use Django admin panel?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

Does Django have admin panel?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.


1 Answers

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.

like image 175
rafalmp Avatar answered Oct 27 '22 04:10

rafalmp