New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error importing RegistrationView when using custom user model with unique USERNAME_FIELD #234
Comments
I need to think about this one. |
I was able to work around this by setting my |
The thing that's worrying me here is that there's no such thing as a truly generic user-registration view/form for Django. The amount of stuff you can do in a custom user model just makes it impossible to write a single form that can handle anything someone might do. And the documentation on using a custom user model with django-registration already makes clear that there are times when you're going to have to write your own stuff rather than be able to use simple subclasses of the built-in forms/views. So I'm leaning toward just doing a better job of documenting what django-registration can support, and maybe better documentation of what you need to do when your user model gets too different from that. |
I think a big problem with this issue is that the errors occur on import time. As it is structured now, not only do I have to write my own form, but I can't use the RegistrationView (because I can't import it) nor the URLs (because those import the RegistrationView as well). It would be nice if these errors could be deferred in some way to when the impacted classes are instantiated or something similar. |
By default, Django will use its own default User model when initializing the CreationForm. This will cause FieldErrors to be raised when a customer user model is provided but doesn't contain the same fields as in Django's default implementation. That's why we use the custom user model to instantiate a CreationForm instead. The implementation can now have any fields it wants as long as it's compatible with Django's AbstractUser. Fixes ubernostrum#234
By default, Django will use its own default User model when initializing the CreationForm. This will cause FieldErrors to be raised when a customer user model is provided but doesn't contain the same fields as in Django's default implementation. That's why we use the custom user model to instantiate a CreationForm instead. The implementation can now have any fields it wants as long as it's compatible with Django's AbstractUser. Fixes ubernostrum#234
Why not use |
Yeah, the issue is that If it's going to change, it'll have to change in a major version bump. I'm thinking through whether I want to do that to cover this case, since it's one that comes up but not super often, and the case of differing significantly from Django's built-in model is already one that has a lot of warnings in the docs. |
I've thought about this a lot, and I think the only way forward is to finally just give up on using Django's That's a backwards-incompatible change, so I can't do it immediately, but once I get a release out for official Django 4.2 compatibility I'll start on a major version bump branch that will ship its own base form that doesn't hard-wire in the default Django |
I believe I'm hitting a bug where I can't import anything from
django_registration.backends.activation.views
because of a bug indjango_registration.forms.RegistrationForm
that is hit at import time.Setup
Create a custom user model deriving from Django's
AbstractBaseUser
. Given it the following properties at a minimum:In your settings.py, set
AUTH_USER_MODEL = 'users.MyUser'
In on of your urls.py, add this import:
from django_registration.backends.activation.views import RegistrationView
This will lead to the following crash when starting Django:
Since this bug in RegistrationForm is happening at import time (and it is imported by RegistrationView), it doesn't matter that I'm following the documentation for custom user models and providing my own RegistrationForm - Django is crashing too early.
I believe the bug is around RegistrationForm's meta class:
Earlier in the file, we have
User = get_user_model()
, which resolves to'users.MyUser'
in this case, withUser.USERNAME_FIELD
andUser.get_email_field_name()
both evaluating to'primary_email'
. However, RegistrationForm's meta class does not override UserCreateForm.Meta's model property, which is set to the Django default User model. This User model does not have aprimary_email
property, leading to the above error.I believe the fix is to set hard code
User
todjango.contrib.auth.models.User
in forms.py. This would be a breaking change however, since people with custom classes would need to provide bothmodel
andfields
in their RegistrationForm's Meta class.The text was updated successfully, but these errors were encountered: