更改 Django admin 的用户 ModelAdmin

发布于 2024-08-27 09:27:50 字数 289 浏览 6 评论 0原文

如何覆盖用户的管理模型?我以为这会起作用,但事实并非如此?

class UserAdmin(admin.ModelAdmin):
    list_display = ('email', 'first_name', 'last_name')
    list_filter = ('is_staff', 'is_superuser')

admin.site.register(User, UserAdmin)

我不想覆盖模板,只需更改显示的字段和内容即可。订购。

请问解决方案?

How do you override the admin model for Users? I thought this would work but it doesn't?

class UserAdmin(admin.ModelAdmin):
    list_display = ('email', 'first_name', 'last_name')
    list_filter = ('is_staff', 'is_superuser')

admin.site.register(User, UserAdmin)

I'm not looking to override the template, just change the displayed fields & ordering.

Solutions please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱的那么颓废 2024-09-03 09:27:50

您必须首先取消注册用户

class UserAdmin(admin.ModelAdmin):
    list_display = ('email', 'first_name', 'last_name')
    list_filter = ('is_staff', 'is_superuser')


admin.site.unregister(User)
admin.site.register(User, UserAdmin)

也许这个问题对您来说也很有趣:Customizing an Admin form in Django while also using自动发现

You have to unregister User first:

class UserAdmin(admin.ModelAdmin):
    list_display = ('email', 'first_name', 'last_name')
    list_filter = ('is_staff', 'is_superuser')


admin.site.unregister(User)
admin.site.register(User, UserAdmin)

Maybe this question is also interesting for you: Customizing an Admin form in Django while also using autodiscover

放血 2024-09-03 09:27:50

使用 Django 3+ 进行了更新测试,

因此您不会丢失密码加密和表单本身等数据,请执行下面的导入

from django.contrib.auth.admin import UserAdmin

定义 AdminCustom 类作为示例,并使用您想要的选项进行自定义,覆盖默认值。

class UserAdminCustom(UserAdmin):
   list_display = ('email', 'first_name', 'last_name', 'is_staff', 'is_superuser')
   list_filter = ('is_staff', 'is_superuser')
   search_fields = ('username', )

最后,按照提到的例子进行操作

admin.site.unregister(User)
admin.site.register(User, UserAdminCustom)

Update tested with Django 3+

So you don't lose data like password encryption and the form itself, perform the import below

from django.contrib.auth.admin import UserAdmin

Define an AdminCustom class as the example and customize with the options you want, overriding the default.

class UserAdminCustom(UserAdmin):
   list_display = ('email', 'first_name', 'last_name', 'is_staff', 'is_superuser')
   list_filter = ('is_staff', 'is_superuser')
   search_fields = ('username', )

And in the end, follow the example mentioned

admin.site.unregister(User)
admin.site.register(User, UserAdminCustom)
灰色世界里的红玫瑰 2024-09-03 09:27:50

正如@haifeng-zhang 在评论中指出的那样,扩展默认的 UserAdmin 是很有用的。

有关此内容的官方文档可以在这里找到:
https://docs.djangoproject .com/en/dev/topics/auth/customizing/#extending-the-existing-user-model

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


# Define a new User admin
class UserAdmin(BaseUserAdmin):
    list_display = ('email', 'first_name', 'last_name')
    list_filter = ('is_staff', 'is_superuser')

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

As pointed by @haifeng-zhang in the comments, it is useful to extend the default UserAdmin instead.

The official documentation about this can be found here:
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model

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


# Define a new User admin
class UserAdmin(BaseUserAdmin):
    list_display = ('email', 'first_name', 'last_name')
    list_filter = ('is_staff', 'is_superuser')

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文