-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,10 +23,11 @@ def url_for_result(self, result): | |
|
|
||
| class TokenAdmin(admin.ModelAdmin): | ||
| list_display = ('key', 'user', 'created') | ||
| list_filter = ('created',) | ||
| fields = ('user',) | ||
| search_fields = ('user__username',) | ||
| search_fields = ('user__%s' % User.USERNAME_FIELD,) | ||
| search_help_text = _('Username') | ||
m000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ordering = ('-created',) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we should keep this
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think default ordering by username combined with a creation date filter makes more sense for several reasons:
But no hard opinion on this. We can stick with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
this make sense to me, and if this is the case then i don't have any problem.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Marking as resolved. Source reference for the fields: # rest_framework/authtoken/models.py::Token
created = models.DateTimeField(_("Created"), auto_now_add=True)
# django/contrib/auth/models.py::AbstractUser
username = models.CharField(
_("username"),
max_length=150,
unique=True, # --> adds index
help_text=_(
"Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only."
),
validators=[username_validator],
error_messages={
"unique": _("A user with that username already exists."),
},
)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I'm late to the party here, but one thing that comes to mind is how this will look in the UI. The ordering is by username but AFAIK the username isn't displayed on the page. If the Not sure if that's a big problem, though. And the point you raised about the lack of index is a good one.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes that sorting might create confusion in user mind. This will be surprise for the users.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @browniebroke The user is displayed. Here is a reference screenshot after the changes.
Apart from the indexing issue, I would prefer ordering by username because if you do a partial match search (e.g. OTOH, if tokens are long-lived, the creation date soon becomes irrelevant: Created at Sept. 2022 vs Oct. 2022 is pretty much the same thing in 2025. But I think we have already spent more energy on this than its worth. Let me know of your final decision on ordering and I'll roll with it. I can also follow-up with adding indexing to created_at, if you feel this is needed. Anecdotally, for the ~500 tokens in our app, the admin loading difference is not noticeable. But I don't know how many tokens other apps may have.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I see, the default I can see some apps where it might be customised as |
||
| ordering = ('user__%s' % User.USERNAME_FIELD,) | ||
m000 marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+28
to
+30
|
||
| actions = None # Actions not compatible with mapped IDs. | ||
|
|
||
| def get_changelist(self, request, **kwargs): | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.