I am facing this FieldError while trying to add User in Users in admin panel.
When I click on add user this error pops up.
I hac=ve not used usable_password in my code anywhere, and tried to multiple method to resolve this issue by deleting db.sqlite3, migration, and then redoing the makemirations. One more thing is that I changed the spelling of class in models.py (after doing migrations and then did migration again it successfully created the migration file). I want to understand why is this happening one more thing i encountered similar error a while back with different project and I don't know how it got resolved that time.
FieldError at /admin/accounts/customuser/add/ Unknown field(s) (usable_password) specified for CustomUser. Check fields/fieldsets/exclude attributes of class CustomUserAdmin. Request Method: GET Request URL: http://127.0.0.1:8000/admin/accounts/customuser/add/ Django Version: 5.1.4 Exception Type: FieldError Exception Value: Unknown field(s) (usable_password) specified for CustomUser. Check fields/fieldsets/exclude attributes of class CustomUserAdmin. Exception Location: C:\Users\Learning Django\blog-api\.venv\Lib\site-packages\django\contrib\admin\options.py, line 841, in get_form Raised during: django.contrib.auth.admin.add_view Python Executable: C:\Users\Learning Django\blog-api\.venv\Scripts\python.exe Python Version: 3.12.2 Python Path: ['C:\\Users\\Desktop\\Learning ' 'Django\\blog-api\\django_project', 'C:\\Python312\\python312.zip', 'C:\\Python312\\DLLs', 'C:\\Python312\\Lib', 'C:\\Python312', 'C:\\Users\\Learning ' 'Django\\blog-api\\.venv', 'C:\\Users\\Learning ' 'Django\\blog-api\\.venv\\Lib\\site-packages']
I am attaching code of models.py, forms.py, and admin.py bellow: models.py
from django.db import models
# Create your models here.
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
name = models.CharField(null = True, blank = True, max_length = 255)
forms.py
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm):
model = CustomUser
fields = UserCreationForm.Meta.fields + ('name',)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields
admin.py
from django.contrib import admin
# Register your models here.
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
change = CustomUserChangeForm
model = CustomUser
list_display = [
"email",
"username",
"name",
"is_staff",
]
fieldsets = UserAdmin.fieldsets + ((None, {"fields": ("name",)}),)
add_fieldsets = UserAdmin.add_fieldsets + ((None, {"fields": ("name",)}),)
admin.site.register(CustomUser, CustomUserAdmin)
Django uses the AdminUserCreationForm
and AdminPasswordChangeForm
for add_form
and change_password_form
respectively.
These forms allow to set the password to unusable. This is for example done to prevent a user from logging in with a password (but for example with an OTP or through other means).
You thus can mix in this form for the UserAdmin
:
from django.contrib.auth.forms import AdminUserCreationForm
class CustomUserCreationForm(AdminUserCreationForm):
class Meta(UserCreationForm):
model = CustomUser
fields = AdminUserCreationForm.Meta.fields + ('name',)