How to properly override User admin in Django

£可爱£侵袭症+ 提交于 2020-03-02 19:32:09

问题


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

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]

Even exclude = ['groups'] raises error:

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."

How to make it work?


回答1:


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.

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



来源:https://stackoverflow.com/questions/45934609/how-to-properly-override-user-admin-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!