Commits

Anonymous committed 68ab1df

added and tested support for JQuery with multiple values

Comments (0)

Files changed (2)

autocomplete/views.py

 
 
 class AutoComplete(object):
-
     def __init__(self):
         self.settings = dict()
 
-    def __call__(self, request, ac_name, query_param='q'):
+    def __call__(self, request, ac_name, query_param=None):
         if not ac_name in self.settings:
             return self.not_found(request, ac_name)
 
-        qs, fields, limit, key, label, auth = self.settings[ac_name]
+        qs, fields, limit, key, label, auth, frontend, query_param = self.settings[ac_name]
         if auth and not request.user.is_authenticated():
             return self.forbidden(request, ac_name)
+
+        if query_param is None:
+            query_param = 'q'
+
         query = request.GET.get(query_param, '')
         
         filter = Q()
         else:
             result = []
             for obj in qs:
-                result.append((getattr(obj, key), label(obj)))
+                if frontend == 'jquery_ui_autocomplete':
+                    itm = {
+                        'id': getattr(obj, key),
+                        'label': label(obj),
+                        'value': label(obj) }
+                else:
+                    itm = (getattr(obj, key), label(obj))
+                result.append(itm)
         return HttpResponse(simplejson.dumps(result),
                 mimetype='application/json')
 
     def register(self, id, queryset, fields, limit=None, key='pk',
-                 label=lambda obj: smart_unicode(obj), auth=False):
-        self.settings[id] = (queryset, fields, limit, key, label, auth)
+                 label=lambda obj: smart_unicode(obj), auth=False, frontend='yui', query_param='q'):
+        self.settings[id] = (queryset, fields, limit, key, label, auth, frontend, query_param)
 
     def not_found(self, request, ac_name):
         return HttpResponse(status=404)
         return HttpResponse(status=403)
 
     def reverse_label(self, ac_name, key_value):
-        qs, fields, limit, key, label, auth = self.settings[ac_name]
+        qs, fields, limit, key, label, auth, frontend, query_param = self.settings[ac_name]
         if key == label:
             return key_value
         if isinstance(label, basestring):

autocomplete/widgets.py

             value = hidden_value = force_unicode(value)
         attrs = flatatt(self.build_attrs(attrs))
         return mark_safe(self.AC_TEMPLATE % locals())
-