changed behaviour for __contains queries and multiple words in search-term
See original GitHub issueIssue Description
I’m creating this as a bug, while of course this could be also an intentional (breaking) change to the library. If that’s the case I would work around it.
Beginning with version 7.6.0
there was a change in search behaviour, originating in in this commit.
The example is
search_fields = {'title__icontains'}
and- using
ModelSelect2Widget
as base for the widget
Now I’m searching for two separate words.
The old behavior (7.5.0
and earlier) was that both words needed to be in the title, so the term was split into the words, while combining the resulting two __icontains
searches with AND
. So in the result for the selectbox we only get the records which contain both words in the title.
New behaviour since 7.6.0
is different, here we also split, but the searches are combined with OR
.
So in the result we have all records that have either one OR the other word in the title
Expected behavior I would have expected the search-results to be the same. While I understand the breaking change in the release, I don’t see why this specific behaviour was changed.
When we take the default django-admin-search as an example (also the base for autocomplete_fields
), we find:
use_distinct = False
search_fields = self.get_search_fields(request)
if search_fields and search_term:
orm_lookups = [construct_search(str(search_field))
for search_field in search_fields]
for bit in search_term.split():
or_queries = [models.Q(**{orm_lookup: bit})
for orm_lookup in orm_lookups]
queryset = queryset.filter(reduce(operator.or_, or_queries))
use_distinct |= any(lookup_needs_distinct(self.opts, search_spec) for search_spec in orm_lookups)
Which means, django admin also does:
- use OR for connecting the searches by field,
- but is uses AND for the search for multiple words in the search-term.
possible solution
If you see this also as a bug, I’m happy to work on a fix for it. Otherwise I would try to work around this behaviour.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:8 (4 by maintainers)
I don’t want to just revert the commit, since it added tests, which are great even if they test for the wrong thing. I will create a patch really quick.
yes 😉