I created a custom user model using the AbstractUser class, without including either the password or usable_password field. However, I encountered the following error in the admin panel when I try to add a new user;
FieldError at /admin/accounts/customuser/add/
Unknown field(s) (usable_password) specified for CustomUser. Check fields/fieldsets/exclude attributes of class CustomUserAdmin.
After investigation, I discovered that the culprit — usable_password — had been automatically added to the default ‘UserAdmin’ class.
This leads me to the following questions:
- Why does my CustomUser model appear to inherit the usable_password field?
- Why does the AbstractUser/AbstractBaseUser class include usable_password, even though it is not a legitimate model field?
- Why is the UserAdmin class attempting to register the default User model, even though my CustomUserAdmin class explicitly defines the fields as:
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ["username", "email", "is_staff", "age"]
fieldsets = UserAdmin.fieldsets + ((None, {"fields": ("age",)}),)
add_fieldsets = UserAdmin.add_fieldsets + ((None, {"fields": ("age",)}),)
admin.site.register(CustomUser, CustomUserAdmin)