#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 (23)
comment:2 by , 9 years ago
Has patch: | set |
---|
follow-up: 9 comment:5 by , 7 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 , 7 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:7 by , 7 years ago
Has patch: | set |
---|
comment:8 by , 7 years ago
Patch needs improvement: | set |
---|
comment:9 by , 5 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 , 3 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 , 3 years 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 , 17 months ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:16 by , 10 months ago
Patch needs improvement: | unset |
---|
comment:17 by , 9 months ago
Patch needs improvement: | set |
---|
comment:18 by , 9 months ago
Patch needs improvement: | unset |
---|
comment:19 by , 8 months ago
Patch needs improvement: | set |
---|
comment:20 by , 8 months ago
Patch needs improvement: | unset |
---|
comment:21 by , 8 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:24 by , 3 months ago
Hi, can you add the possibility of disabling casting logic? After updating to django 5.2 I was faced with a crash when using search on different admin models pages.
For example, I have an integer Filed, which is indexed in the database:
thread_id = models.IntegerField(null=False,db_index=True,)
I use this field in ModelAdmin search_fields = ('thread_id__exact',)
Django 5.1.8 generates correct SQL which use index
... WHERE "reviews"."thread_id" = 1123 ...
But django 5.2 generates incorrect SQL which doesn't use the database index, my search request failed with timeout and the database has an extra load
... WHERE ("reviews"."thread_id")::varchar = '1123' ...
PR: https://github.com/django/django/pull/6219