Opened 5 years ago

Closed 5 years ago

#17039 closed Bug (duplicate)

search_fields and multiword searches

Reported by: Sergiy Kuzmenko Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords:
Cc: Sergiy Kuzmenko Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


Django admin automatically splits query string for search_fields and creates a chained series of filters. For example if ModelAdmin instance contains

search_fields = ('my_field',)

and the user searches for "cool search", the resulting queryset will look like this:


translating into SQL

 ... WHERE my_field LIKE '%cool%' AND my_field LIKE '%search%'

So far so good. Now if we have anchored or exact search instead:

search_fields = ('^my_field',)

Django admin will do exactly the same thing as above and will result in a query like this:

 ... WHERE my_field LIKE 'cool%' AND my_field LIKE 'search%'

which is not ever going to find anything because the same field cannot possibly start with two different strings.

For this reason I am proposing to inspect lookup first and don't split query string if exact or anchored lookup is requested. So we would have instead:

 ... WHERE my_field LIKE 'cool search%'

The code that need to change is between these two lines: and

Change History (2)

comment:1 Changed 5 years ago by Sergiy Kuzmenko

Cc: Sergiy Kuzmenko added

comment:2 Changed 5 years ago by Karen Tracey

Resolution: duplicate
Status: newclosed

I think this is #6933.

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