Ticket #15203: admin-search.diff

File admin-search.diff, 1.5 KB (added by wkornewald, 4 years ago)

patch against trunk

  • django/contrib/admin/views/main.py

    diff -r 248bfcafb5d1 django/contrib/admin/views/main.py
    a b  
    241241                return "%s__icontains" % field_name
    242242
    243243        if self.search_fields and self.query:
    244             for bit in self.query.split():
    245                 or_queries = [models.Q(**{construct_search(str(field_name)): bit}) for field_name in self.search_fields]
    246                 qs = qs.filter(reduce(operator.or_, or_queries))
     244            search_queries = [models.Q(**{construct_search(field_name): self.query})
     245                              for field_name in map(str, self.search_fields)
     246                              if field_name.startswith('=')]
     247            if len(self.search_fields) > 1 or not search_queries:
     248                and_queries = []
     249                for bit in self.query.split():
     250                    or_queries = [models.Q(**{construct_search(field_name): bit})
     251                                  for field_name in map(str, self.search_fields)]
     252                    and_queries.append(reduce(operator.or_, or_queries))
     253                if and_queries:
     254                    search_queries.append(reduce(operator.and_, and_queries))
     255            if search_queries:
     256                qs = qs.filter(reduce(operator.or_, search_queries))
    247257            for field_name in self.search_fields:
    248258                if '__' in field_name:
    249259                    qs = qs.distinct()
Back to Top