How to properly override User admin in Django(如何在 Django 中正确覆盖用户管理员)
问题描述
我想在 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.
这篇关于如何在 Django 中正确覆盖用户管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Django 中正确覆盖用户管理员
基础教程推荐
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
