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?
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 .
The admin site will default to the ordering specified on your model itself, eg
class MyUserModel:
created = models.DateTimeField()
class Meta:
ordering = ('created', )
If you want something more flexible, that is, if you want to use Django default user model without subclassing it , take a look at https://docs.djangoproject.com/ja/1.9/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display
--
Edit: While what I say is not wholly incorrect, @rafalmp's answer is the right one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.