Code

Ticket #2559: django-search_fields-operators.diff

File django-search_fields-operators.diff, 1.3 KB (added by Andy Dustman <farcepest@…>, 8 years ago)

Implementation

Line 
1Index: django/contrib/admin/views/main.py
2===================================================================
3--- django/contrib/admin/views/main.py  (revision 3599)
4+++ django/contrib/admin/views/main.py  (working copy)
5@@ -711,9 +711,19 @@
6         qs = qs.order_by((self.order_type == 'desc' and '-' or '') + lookup_order_field)
7 
8         # Apply keyword searches.
9+        def construct_search(field_name):
10+            if field_name.startswith('^'):
11+                return "%s__istartswith" % field_name[1:]
12+            elif field_name.startswith('='):
13+                return "%s__iexact" % field_name[1:]
14+            elif field_name.startswith('@'):
15+                return "%s__search" % field_name[1:]
16+            else:
17+                return "%s__icontains" % field_name
18+           
19         if self.lookup_opts.admin.search_fields and self.query:
20             for bit in self.query.split():
21-                or_queries = [models.Q(**{'%s__icontains' % field_name: bit}) for field_name in self.lookup_opts.admin.search_fields]
22+                or_queries = [models.Q(**{construct_search(field_name): bit}) for field_name in self.lookup_opts.admin.search_fields]
23                 other_qs = QuerySet(self.model)
24                 other_qs = other_qs.filter(reduce(operator.or_, or_queries))
25                 qs = qs & other_qs