-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Update TokenAdmin to respect USERNAME_FIELD of the user model. #9836
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
base: main
Are you sure you want to change the base?
Conversation
a0161c4 to
5ff42dc
Compare
5ff42dc to
941cc57
Compare
941cc57 to
fb633f4
Compare
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.
Pull request overview
This PR updates the TokenAdmin class to properly support custom user models with different username fields. Django allows projects to customize the username field via USERNAME_FIELD, and the admin interface previously hardcoded 'username' instead of respecting this setting.
Key changes:
- Updated
search_fieldsandorderingto dynamically useUser.USERNAME_FIELDinstead of hardcoded 'username' - Added
list_filterfor the 'created' field - Changed default ordering from descending creation date to ascending username field
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| search_fields = ('user__%s' % User.USERNAME_FIELD,) | ||
| search_help_text = _('Username') | ||
| ordering = ('-created',) | ||
| ordering = ('user__%s' % User.USERNAME_FIELD,) |
Copilot
AI
Nov 26, 2025
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.
The changes to use USERNAME_FIELD instead of hardcoded 'username' lack test coverage. Consider adding tests that verify the admin configuration works correctly with a custom user model that uses a different USERNAME_FIELD (e.g., 'email'), ensuring search and ordering function as expected.
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.
@m000 it would be great to add test coverage for the changes.
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.
Added. It took a while to figure it out. Admin tests are a major PITA because modules include code that runs on load.
fb633f4 to
dd3e82d
Compare
p-r-a-v-i-n
left a comment
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.
Thanks for adding test coverage.
Admin tests are a major PITA because modules include code that runs on load.
😄 I'm happy you did it. Nice work :) . well this part of test and code also new for me.
other than this everything looks good to me , I think the precommit hook is failing fix it and lets wait for Bruno and Auvi for feedback.
dd3e82d to
d78b161
Compare
- Respect USERNAME_FIELD of the user model. - Default ordering by username. - Filter by creation date.
Thanks for the hint. I had missed the existence of the pre-commit hooks. I had chomped an extra line and isort complained. It should be good now. |
d78b161 to
77be42e
Compare
Description
TokenAdmin assumed a fixed username field for the model specified by
settings.AUTH_USER_MODEL. However, django allows using some other field as the username, and this is specified by overriding theUSERNAME_FIELDproperty. See [1].Also, switched default ordering to also use the username, and added filter for creation date.
[1] https://docs.djangoproject.com/en/5.2/topics/auth/customizing/#django.contrib.auth.models.CustomUser.USERNAME_FIELD