Opened 4 years ago

Closed 4 years ago

#17039 closed Bug (duplicate)

search_fields and multiword searches

Reported by: shelldweller Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords:
Cc: shelldweller 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 Changed 4 years ago by shelldweller

  • Cc shelldweller added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 4 years ago by kmtracey

  • Resolution set to duplicate
  • Status changed from new to closed

I think this is #6933.

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