1. Luke Plant
  2. django-selectable


Mark Lavin  committed 54628c8

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

  • Participants
  • Parent commits 4ecbb3a
  • Branches multi-field-lookups

Comments (0)

Files changed (2)

File selectable/base.py

View file
  • Ignore whitespace
+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):

File selectable/registry.py

View file
  • Ignore whitespace
 from django.utils.encoding import force_unicode
-from selectable.base import LookupBase
+from selectable.base import LookupBase, ModelLookup
 from selectable.exceptions import (LookupAlreadyRegistered, LookupNotRegistered,
     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):