1đź‘Ť
âś…
jQueryUI part:
$("#id_of_your_input").keypress(function(){
$(this).autocomplete({
source: $(this).data('url'), ## or hard code it: source: '/api/...'
minLength: 2,
delay:300,
select: function(event, ui) {
$('#id_of_your_drop_down').val(ui.item.value);
}
});
});
The view which handles your $(this).data('url')
:
import json
NUMBER_OF_RESULTS = 5
def view_for_your_api(request):
if not request.is_ajax():
return HttpResponse('false')
results = model.objects.all()[:NUMBER_OF_RESULTS]
data = json.dumps([{'label': r.name, 'value': r.name} for r in results])
return HttpResponse(data, 'application/json')
I assumed you have r.name
in your model. Replace it with your field or your neccessities
👤Mihai Zamfir
0đź‘Ť
This is a good idea “Get multiple autocomplete suggestions”.
Here is how autocomplete works.
For example whenever you type something in a text box the AJAX runs and returns particular result for that specific texts.
To get multiple autocompletes..
If you want to do that multiple autocomplete you need to save multiple data or tags in a column according to repeated occurances. or make a “*one to many relationship” in the database and return all those values.
👤Wickkiey
- [Answer]-GenericForeinKey retrieve field value in models.py
- [Answer]-How do I filter this in Django ORM
- [Answer]-Django MySQL Exact Word Match With iRegex Search
Source:stackexchange.com