Fix #1159: Fallback to USERNAME_FIELD when AXES_USERNAME_FORM_FIELD not in credentials#1414
Open
Dzhud wants to merge 3 commits intojazzband:masterfrom
Open
Fix #1159: Fallback to USERNAME_FIELD when AXES_USERNAME_FORM_FIELD not in credentials#1414Dzhud wants to merge 3 commits intojazzband:masterfrom
Dzhud wants to merge 3 commits intojazzband:masterfrom
Conversation
aleksihakli
requested changes
Mar 20, 2026
| "Using parameter credentials to get username with key settings.AXES_USERNAME_FORM_FIELD" | ||
| ) | ||
| return credentials.get(settings.AXES_USERNAME_FORM_FIELD, None) # type: ignore[return-value] | ||
| username = credentials.get(settings.AXES_USERNAME_FORM_FIELD, None) |
Member
There was a problem hiding this comment.
This code is 100% duplicated and could be written as a function, could you refactor this and add a unit test for fetching the username?
Author
There was a problem hiding this comment.
Ah, thanks for the feedback! You're absolutely right about the duplication.
I've refactored the code to extract the username lookup logic into a new get_username_from_data(data: dict) -> Optional[str] helper function. Now get_client_username() calls this helper for both credentials and request data lookups, eliminating the duplication.
Changes in this update:
- New get_username_from_data() function in helpers.py
- Added 5 unit tests in
GetUsernameFromDataTestCase:
2.1test_get_username_from_data_with_matching_field
2.2test_get_username_from_data_fallback_to_username_field
2.3test_get_username_from_data_custom_field_takes_priority
2.4test_get_username_from_data_returns_none_when_not_found
2.5test_get_username_from_data_with_empty_dict
All tests pass (18 total for username handling) and mypy reports no issues.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #1159: Fallback to USERNAME_FIELD when AXES_USERNAME_FORM_FIELD not in credentials
Summary
When
AXES_USERNAME_FORM_FIELDis set to a custom value (e.g.,"auth-username"), theAccessAttemptrecord wasn't getting the username populated because Django'suser_login_failedsignal sends credentials usingUSERNAME_FIELD(typically"username"), not the custom field name.Changes
get_client_username()inaxes/helpers.pyto add fallback logic:AXES_USERNAME_FORM_FIELDget_user_model().USERNAME_FIELDif not founddocs/4_configuration.rstto describe the fallback behaviorHow it works
Tests
Wrote 3 new tests to cover this fix:
test_get_client_username_fallback_to_username_field_from_credentialsUSERNAME_FIELDtest_get_client_username_fallback_to_username_field_from_requestrequest.POSTdatatest_get_client_username_custom_field_takes_priorityVerification
Fixes #1159