def auto_complete(request, queryset, fields=None):
 object_list = []
 limit = request.GET.get('limit', 10)
 query = request.GET.get('term', '')
 if fields:
  q_object = Q()
  for field in fields:
   q_object |= Q(**{field: query})
  queryset = queryset.filter(q_object)
 for obj in queryset[:limit]:
  object_list.append({'text': obj.__unicode__(), 'id': obj.pk})
 return HttpResponse(json.dumps(object_list), mimetype='application/json')

def auto_complete ( request , queryset , fields = None ) : object_list = [ ] limit = request . GET . get ( 'limit' , 10 ) query = request . GET . get ( 'term' , '' ) if fields : q_object = Q ( ) for field in fields : q_object |= Q ( ** { field : query } ) queryset = queryset . filter ( q_object ) for obj in queryset [ : limit ] : object_list . append ( { 'text' : obj . __unicode__ ( ) , 'id' : obj . pk } ) return HttpResponse ( json . dumps ( object_list ) , mimetype = 'application/json' )
returns a json list to be used with the autocompletewidget javascript .