-
-
Notifications
You must be signed in to change notification settings - Fork 795
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
Change admin search_fields to favor USERNAME_FIELD
instead of "email".
#1428
base: master
Are you sure you want to change the base?
Conversation
First nothing guarantees that the user model has a field named "email" as it can be set to a different name using `EMAIL_FIELD`. At the very least the `get_email_field_name` should have been used. Secondly nothing guarantees that `EMAIL_FIELD` is going to be indexed and thus suitable for search purposes. On the other hand `USERNAME_FIELD` must be unique and thus indexed to enforce the constraint and unique identifies users. For these reasons `USERNAME_FIELD` represents a better choice to allow the different toolkit models to be searched by through the admin.
6019cbe
to
0206587
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.
In further thinking about this, I see a problem with this approach:
By unilaterally changing search behavior from user__email
to user__username
this creates a breaking change for anybody already expecting the admin search to use user__email
.
How about doing this:
- Implement the fix you recommend of calling the get_email_field_name method instead of looking for the
email
attribute (which in all fairness is how it is documented in the contrib admin models documentation!) - Add (and document) a configuration setting that lists the fields to search.
- Add a corresponding setting (or code) to set search_help_text that describes what is searched.
This will allow you to configure searching only by username for your performance reason use case while retaining the existing code default of searching by email and further allowing other users to expand the list of admin search fields for their particular use cases.
That's a valid point that might be worth raising upstream. I'll see what I can do. I'm happy to submit a PR for the first point as it's unambiguously a good change but I'm not convinced about the other ones. There doesn't exist a setting today and Django already provides a way to override registered Django admins class ApplicationAdmin(admin.ApplicationAdmin):
search_fields = ("name", "user__email")
search_help_text = "Search applications by name and user email"
admin.site.register(application_model, ApplicationAdmin) In fact that's the approach we used at $JOB when we had to make sure IMO settings like To summarize I think we should either proceed with this PR as it is as |
In my case the It's possible to work around this by using However, maybe DOT should just drop the ability to search by email. Instead it can document how to customize the admin classes instead, using It would be a breaking change, but it is not a "mission critical" feature. Those that need it can bring it back using their own code. The PR that first tried to introduce this had a fitting comment by @IvanAnishchuk :
P.S. @charettes I didn't even think about the |
Description of the Change
Per this request.
First nothing guarantees that the user model has a field named "email" as it can be set to a different name using
EMAIL_FIELD
. At the very least theget_email_field_name
method should have been used.Secondly nothing guarantees that
EMAIL_FIELD
is going to be indexed and thus suitable for search purposes. On the other handUSERNAME_FIELD
must be unique and thus indexed in order to enforce this constraint.For these reasons
USERNAME_FIELD
represents a better choice to allow the different toolkit models to be searched by through the admin.Checklist
CHANGELOG.md
updated (only for user relevant changes)AUTHORS