如何在 Django 中正确覆盖用户管理员

2022-01-25 00:00:00 python django django-admin

问题描述

我想在 Django 管理中添加添加内联模型并从用户更改表单中排除一些字段.

I want to add add inline model and exclude some fields from User change form in Django admin.

我正在尝试覆盖 Django 的内置 UserAdmin 以保留用户更改设计:

I'm trying to override Django's built-in UserAdmin to preserve User change design:

class UserCustomAdmin(UserAdmin):
    # list_display = ['id', 'username','email', 'last_login']
    exclude = ['groups','user_permissions']
    inlines = [UserProfileInline]

即使 exclude = ['groups'] 也会引发错误:

Even exclude = ['groups'] raises error:

u"在 'UserForm' 中找不到键 'groups'.选项有:date_joined,电子邮件、名字、is_active、is_staff、is_superuser、last_login、姓氏、密码、用户名."

u"Key 'groups' not found in 'UserForm'. Choices are: date_joined, email, first_name, is_active, is_staff, is_superuser, last_login, last_name, password, username."

如何让它发挥作用?


解决方案

groups 字段出现在 UserAdmin.fieldsets 也是.我认为出现错误是因为您从表单中排除了该字段,但后来在字段集中定义并且表单失败.

groups field appears in the UserAdmin.fieldsets also. The error appears, I think, because you exclude the field from form, but later is defined in the fieldsets and form fails.

尝试在没有 groups 字段的 UserCustomAdmin 中相应地覆盖 fieldsets.

Try to overwrite the fieldsets accordingly, in your UserCustomAdmin with no groups field.

相关文章