python 如何在 Django CRUD 中自定义 auth.User 管理页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2270537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to customize the auth.User Admin page in Django CRUD?
提问by Natim
I just want to add the subscription date in the User list in the Django CRUD?Administration site. How can I do that ?
我只想在 Django CRUD?Administration 站点的用户列表中添加订阅日期。我怎样才能做到这一点 ?
Thank you for your help
谢谢您的帮助
回答by Natim
I finally did like this in my admin.py file :
我终于在我的 admin.py 文件中这样做了:
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)
回答by Fernando Freitas Alves
Another way to do this is extending the UserAdmin class.
另一种方法是扩展 UserAdmin 类。
You can also create a function to put on list_display
您还可以创建一个函数来放置 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)
回答by Joshua Partogi
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
假设您的用户类是User
并且您的订阅日期字段是subscription_date
,这就是您需要添加到您的admin.py
class UserAdmin(admin.ModelAdmin):
list_display = ('subscription_date',)
admin.site.register(User, UserAdmin)