Opened 13 years ago

Closed 13 years ago

#17039 closed Bug (duplicate)

search_fields and multiword searches

Reported by: Sergiy Kuzmenko Owned by: nobody
Component: contrib.admin Version: dev
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

Description

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:

qs.filter(my_field__icontains="cool").filter(my_field__icontains="search")

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:
https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/views/main.py#L362 and
https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/views/main.py#L365

Change History (2)

comment:1 by Sergiy Kuzmenko, 13 years ago

Cc: Sergiy Kuzmenko added

comment:2 by Karen Tracey, 13 years ago

Resolution: duplicate
Status: newclosed

I think this is #6933.

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