DEV Community

Will Vincent
Will Vincent

Posted on • Originally published at learndjango.com

Fixing Django FieldError at /admin/accounts/customuser/add/

If you are a Django developer who wants to add a custom user model to your project, you've likely come across this error on Django versions 5.0 and above.

FieldError at /admin/accounts/customuser/add/
Unknown field(s) (usable_password) specified for CustomUser. Check fields/fieldsets/exclude attributes of class CustomUserAdmin.

FieldError Message

The issue is around UserCreationForm. In Django versions up to 4.2, you could set your accounts/forms.py file to add updated user creation and change forms.

# accounts/forms.py
from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from .models import CustomUser

class CustomUserCreationForm(UserCreationForm):

    class Meta:
        model = CustomUser
        fields = ("username", "email")

class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = ("username", "email")
Enter fullscreen mode Exit fullscreen mode

However, as of Django 5.0, that leads to the above-mentioned FieldError. The fix is straightforward to do, thankfully, which is to swap out UserCreationForm for the newer AdminUserCreationForm instead, which includes the additional usable_password field causing the initial issue.

# accounts/forms.py
from django.contrib.auth.forms import AdminUserCreationForm, UserChangeForm  # new

from .models import CustomUser


class CustomUserCreationForm(AdminUserCreationForm):  # new

    class Meta:
        model = CustomUser
        fields = ("username", "email")


class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = ("username", "email")
Enter fullscreen mode Exit fullscreen mode

You can see the related ticket #35678 and forum discussion.

For a complete guide on using a custom user model in Django, refer to this tutorial.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay