Opened 8 years ago

Last modified 2 months ago

#26001 assigned Cleanup/optimization

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: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
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 (14)

comment:3 by Simon Charette, 8 years ago

I think we should favor the approach suggested in #26184 instead.

comment:4 by Tim Graham, 8 years ago

Patch needs improvement: set

I agree, let's revisit this if it doesn't.

comment:5 by Tim Graham, 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 Nick Sandford, 6 years ago

Owner: changed from nobody to Nick Sandford
Status: newassigned

comment:7 by Nick Sandford, 6 years ago

Has patch: set

comment:8 by Tim Graham, 6 years ago

Patch needs improvement: set

in reply to:  5 comment:9 by Niclas Olofsson, 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 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.

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.

Instead of trying to catch these errors from .filter() as in the previous patch, we could run to_python() for each search field in order to test if that particular field would raise an exception later on when the query is run.

Last edited 4 years ago by Niclas Olofsson (previous) (diff)

comment:10 by Mariusz Felisiak, 17 months ago

Owner: Nick Sandford removed
Status: assignednew
Summary: Make ModelAdmin.search_fields do an integer lookup for IntegerFieldsMake ModelAdmin.search_fields raise data errors on __exact lookups for non-string fields.

comment:11 by Mariusz Felisiak, 17 months ago

#34191 was a duplicate for DecimalField.

in reply to:  11 comment:12 by Myhailo Chernyshov, 16 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.

Last edited 16 months ago by Myhailo Chernyshov (previous) (diff)

comment:13 by Sandeep Pal, 3 months ago

can i work on this?

comment:14 by Saurabh , 3 months ago

Owner: set to Saurabh
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top