Skip to main content
 首页 » 编程设计

python之按在 django 管理面板中创建的日期排序用户

2025年05月04日32wayfarer

如何在 django 管理面板中对用户进行排序,以便在显示时按创建日期排序?目前它们按字母顺序列出

我知道我可以通过以下方式导入 User 模型:from django.contrib.auth.models import User

我该怎么做?

请您参考如下方法:

要更改管理面板中用户的默认顺序,您可以继承默认的 UserAdmin 类。在您的应用程序的 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) 

有关更多信息,请参阅 documentation .