search_fields and multiword searches
|Reported by:||shelldweller||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||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: