Mark Lavin avatar Mark Lavin committed 54628c8

Started adding multi-field support for ModelLookup. See #36.

Comments (0)

Files changed (2)

selectable/base.py

+import operator
 import re
 
 from django.conf import settings
 from django.core.paginator import Paginator, InvalidPage, EmptyPage
 from django.core.urlresolvers import reverse
 from django.core.serializers.json import DjangoJSONEncoder
+from django.db.models import Q
 from django.http import HttpResponse
 from django.utils import simplejson as json
 from django.utils.encoding import smart_unicode
     model = None
     filters = {}
     search_field = ''
+    search_fields = ()
 
     def get_query(self, request, term):
         qs = self.get_queryset()
-        if term and self.search_field:
-            qs = qs.filter(**{self.search_field: term})
+        if term:
+            search_filters = []
+            if self.search_field and not self.search_fields:
+                self.search_fields = (self.search_field, )
+            if self.search_fields:
+                for field in self.search_fields:
+                    search_filters.append(Q(**{field: term}))
+            qs = qs.filter(reduce(operator.or_, search_filters))
         return qs
 
     def get_queryset(self):

selectable/registry.py

 from django.utils.encoding import force_unicode
 
-from selectable.base import LookupBase
+from selectable.base import LookupBase, ModelLookup
 from selectable.exceptions import (LookupAlreadyRegistered, LookupNotRegistered,
                                     LookupInvalid)
 
     def validate(self, lookup):
         if not issubclass(lookup, LookupBase):
             raise LookupInvalid(u'Registered lookups must inherit from the LookupBase class')
+        if issubclass(lookup, ModelLookup) and getattr(lookup, 'search_field'):
+            import warnings
+            warnings.warn(
+                u"ModelLookup.search_field is deprecated; Use ModelLookup.search_fields instead", 
+                PendingDeprecationWarning
+            )
 
     def register(self, lookup):
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.