-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Support basic authentication with custom user models that change username field #2952
Conversation
…name field Support basic authentication with custom user models with a username field that is not named 'username'.
Compatibility code for getting user model
Import get_user_model from compat module to be compatible with older django versions (e.g. 1.4).
Using the Looks okay, but I might need a little more persuading of the necessity for the change. |
Support User model in Django 1.4 that has not a USERNAME_FIELD attribute.
It works fine with Django's ModelBackend but it may not work with other backends, so better be safer than sorry. Particularly, it does not work with django-python3-ldap. According to Django's documentation, an authentication backend is required to implement authenticate(**credentials), which does not dictate the parameter names. That said, using USERNAME_FIELD instead of a hardcoded 'username' is a safer approach imho. |
if hasattr(user_model, 'USERNAME_FIELD'): | ||
username_field = user_model.USERNAME_FIELD | ||
else: | ||
username_field = 'username' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could slim this down with username_field = getattr(user_model, 'USERNAME_FIELD', 'username')
I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just addressed that
Fix flake8 error
On Tom's suggestion, improve coding style by using a single-line call to getattr() with a default value instead of a multi-line if/else clause.
Support basic authentication with custom user models that change username field
Ace! ✨ |
Support basic authentication with custom user models with a username field that is not named 'username'.