Skip to content

changed behaviour for __contains queries and multiple words in search-term #39

Closed
@syphar

Description

@syphar

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:

https://github.com/django/django/blob/3668da8de821fcb4ddd83fb50a1f04aa02600894/django/contrib/admin/options.py#L1020-L1030

        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.

Metadata

Metadata

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions