问题
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