Opened 10 years ago

Closed 8 months ago

Last modified 3 months ago

#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:3 by Simon Charette, 9 years ago

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

comment:4 by Tim Graham, 9 years ago

Patch needs improvement: set

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

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

Owner: changed from nobody to Nick Sandford
Status: newassigned

comment:7 by Nick Sandford, 7 years ago

Has patch: set

comment:8 by Tim Graham, 7 years ago

Patch needs improvement: set

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

Version 0, edited 5 years ago by Niclas Olofsson (next)

comment:10 by Mariusz Felisiak, 3 years 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, 3 years ago

#34191 was a duplicate for DecimalField.

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

Last edited 3 years ago by Myhailo Chernyshov (previous) (diff)

comment:13 by Sandeep Pal, 18 months ago

can i work on this?

comment:14 by Saurabh , 17 months ago

Owner: set to Saurabh
Status: newassigned

comment:16 by John Beimler, 10 months ago

Patch needs improvement: unset

comment:17 by Sarah Boyce, 9 months ago

Patch needs improvement: set

comment:18 by Sarah Boyce, 9 months ago

Patch needs improvement: unset

comment:19 by Sarah Boyce, 8 months ago

Patch needs improvement: set

comment:20 by Sarah Boyce, 8 months ago

Patch needs improvement: unset

comment:21 by Sarah Boyce, 8 months ago

Triage Stage: AcceptedReady for checkin

comment:22 by Sarah Boyce <42296566+sarahboyce@…>, 8 months ago

Resolution: fixed
Status: assignedclosed

In f223729:

Fixed #26001 -- Fixed non-string field exact lookups in ModelAdmin.search_fields.

comment:23 by Sarah Boyce <42296566+sarahboyce@…>, 8 months ago

In 5fa4ccab:

Refs #26001 -- Handled relationship exact lookups in ModelAdmin.search_fields.

comment:24 by Oleksandr, 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' ...

Note: See TracTickets for help on using tickets.
Back to Top