1👍
✅
not tested but I think you need to do this:
def paper_autocompleate(request):
if request.is_ajax():
q = request.GET.get('term', '')
autoPaperwork = Paperwork.objects.filter(
Q(title__icontains=q) | Q(description__icontains=q))[:10]
results = []
for p in autoPaperwork:
results.append(p.title)
data = json.dumps(results)
else:
data = 'fail'
mimetype = 'application/json'
return HttpResponse(data, content-type=mimetype)
not sure what your p_json
was doing, but my JQuery-UI autocomplete works via jsut a simply json list of
["result 1", "result 2"]
which I get by using json.dumps(<python_list_object>)
edit: here is my code for a generic get sorted autocompete of any db object field
obj = apps.get_model(app_label=app_name, model_name=model_name)
#obj = locate('LessonApp.models.%s' % otype) # old way of locating
try:
search_list = list(obj.objects.\
exclude(Q(**{field: ''}) | Q(**{'{0}__isnull'.format(field): True})).\
values_list(field, flat=True).distinct().order_by(field))
if q:
search_list = filter(lambda s: s[:len(q)].lower() == q.lower(), search_list) # search the list for q, ignore case
return HttpResponse(json.dumps(search_list), content_type='application/json')
except Exception as ex:
return HttpResponse('{"result": "error", "error": "DB Error\n%s"}' % ex, content_type='application/json')
EDIT: here is my javascript to call my input
$('#id_group_name').autocomplete({
source: "/autocomplete/cec_cms/CmsNews/group_name",
minLength: 0,
delay: 500 //default is 300
}).focus(function() {
$(this).autocomplete('search', $(this).val());
});
Source:stackexchange.com