#26001 closed Cleanup/optimization (fixed)
Make ModelAdmin.search_fields raise data errors on __exact lookups for non-string fields.
Reported by: | Tim Graham | Owned by: | Saurabh |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Currently, all queries are done as string lookups which gives something like this on PostgreSQL, for example: ("admin_views_pluggablesearchperson"."age"::text) = UPPER(20))
. It would be more efficient if the admin cast the search value to an integer and used that for the query.
Change History (22)
comment:2 by , 9 years ago
Has patch: | set |
---|
follow-up: 9 comment:5 by , 6 years ago
Has patch: | unset |
---|---|
Patch needs improvement: | unset |
As far as I tested, you can now use something like search_fields = ['votes__exact']
to do the appropriate query, however, if a non-integer search term is entered, the page crashes with ValueError invalid literal for int() with base 10: 'abc'
from IntegerField.get_prep_value()
. Probably that exception should be caught in ModelAdmin.get_search_results()
and no results returned.
comment:6 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:7 by , 6 years ago
Has patch: | set |
---|
comment:8 by , 6 years ago
Patch needs improvement: | set |
---|
comment:9 by , 4 years ago
Replying to Tim Graham:
As far as I tested, you can now use something like
search_fields = ['votes__exact']
to do the appropriate query, however, if a non-integer search term is entered, the page crashes withValueError invalid literal for int() with base 10: 'abc'
fromIntegerField.get_prep_value()
. Probably that exception should be caught inModelAdmin.get_search_results()
and no results returned.
We have to consider that there might be multiple search fields of mixed types though, i.e. search_fields = ['votes__exact', 'title', 'text__contains']
, just so the matches for other fields does not get discarded just because one of the fields threw an error.
comment:10 by , 2 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
Summary: | Make ModelAdmin.search_fields do an integer lookup for IntegerFields → Make ModelAdmin.search_fields raise data errors on __exact lookups for non-string fields. |
comment:12 by , 23 months ago
Replying to Mariusz Felisiak:
#34191 was a duplicate for
DecimalField
.
You can use iexact
instead of exact
for non-string fields, I think, it even makes sense, because the word exact
means complete equality even with type. It works for me.
comment:14 by , 9 months ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:16 by , 7 weeks ago
Patch needs improvement: | unset |
---|
comment:17 by , 4 weeks ago
Patch needs improvement: | set |
---|
comment:18 by , 3 weeks ago
Patch needs improvement: | unset |
---|
comment:19 by , 2 weeks ago
Patch needs improvement: | set |
---|
comment:20 by , 2 weeks ago
Patch needs improvement: | unset |
---|
comment:21 by , 2 weeks ago
Triage Stage: | Accepted → Ready for checkin |
---|
PR: https://github.com/django/django/pull/6219