Code

Ticket #15203: admin-search.diff

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

patch against trunk

Line 
1diff -r 248bfcafb5d1 django/contrib/admin/views/main.py
2--- a/django/contrib/admin/views/main.py        Thu Jan 20 16:26:50 2011 +0100
3+++ b/django/contrib/admin/views/main.py        Tue Feb 01 17:28:03 2011 +0100
4@@ -241,9 +241,19 @@
5                 return "%s__icontains" % field_name
6 
7         if self.search_fields and self.query:
8-            for bit in self.query.split():
9-                or_queries = [models.Q(**{construct_search(str(field_name)): bit}) for field_name in self.search_fields]
10-                qs = qs.filter(reduce(operator.or_, or_queries))
11+            search_queries = [models.Q(**{construct_search(field_name): self.query})
12+                              for field_name in map(str, self.search_fields)
13+                              if field_name.startswith('=')]
14+            if len(self.search_fields) > 1 or not search_queries:
15+                and_queries = []
16+                for bit in self.query.split():
17+                    or_queries = [models.Q(**{construct_search(field_name): bit})
18+                                  for field_name in map(str, self.search_fields)]
19+                    and_queries.append(reduce(operator.or_, or_queries))
20+                if and_queries:
21+                    search_queries.append(reduce(operator.and_, and_queries))
22+            if search_queries:
23+                qs = qs.filter(reduce(operator.or_, search_queries))
24             for field_name in self.search_fields:
25                 if '__' in field_name:
26                     qs = qs.distinct()