Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
Modify ObtainAuthToken to use the User model's USERNAME_FIELD and password for authentication instead of assuming username and password.#9674
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
…password' instead of 'username' and 'password' for both the built-in and custom User models
If any changes are required, please let me know. |
I'm not sure this falls under ourcurrent maintenance policy:
One could argue that this improves compatibility with custom Django user models, but on the other hand It's also simple to customise in user-land right now, and this is explained in our docs. If we were to ever accept it, we would need some tests to cover the behaviour with a customised user model... |
partho-debnath commentedMar 31, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Thank you for reviewing my pull request and for your feedback. I understand that Django REST Framework is considered feature-complete and that new features are usually only accepted if they align with Django’s ongoing development. However, I see this change as more of a compatibility improvement rather than a new feature. While @browniebroke, Are you saying that it won't be possible to accept or merge this change? |
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.
I think this might break existing projects or functionalities
Summary
Django’s default user model uses
username
andpassword
for authentication. However, when a custom user model is defined withUSERNAME_FIELD = 'email'
, Django correctly uses email and password for authentication.The issue was thatDjango REST Framework's
obtain_auth_token
endpoint (used for TokenAuthentication) still expectedusername
andpassword
, even when a custom user model usedemail
instead ofusername
.Fix
This update modifies the
ObtainAuthToken
view to dynamically useUSERNAME_FIELD
instead of assumingusername
. Now, authentication works consistently, whether using the built-in User model or a custom one, with no inconsistencies.Changes Made:
ObtainAuthToken
now retrieves theUSERNAME_FIELD
from the user model.USERNAME_FIELD
andpassword
, ensuring compatibility with both built-in and custom user models.Impact
USERNAME_FIELD = 'email'
) can now log in usingemail
andpassword
instead ofusername
andpassword
.default User
model or acustom User
model whereUSERNAME_FIELD = 'username'
can continue logging in usingusername
andpassword
as expected.