Reputation: 4285
i want to create a api using django rest framework which will do some advance search, say a list of grocery item is saved in database and user want to create a list of grocery and he enters first 2 or 3 letters and then my api will do query to get rest of the item name and will suggest to user
I have seen the documentation of drf haystack but not sure whether will it fulfill my requirement. Also it does not support django LTS version 1.11 .
Can you please give me some suggestion? is django rest framework itself provide any support to create such a api which will do such advance search
which i mentioned above? i just need some suggestion as i am new in django rest framework.
Upvotes: 2
Views: 1700
Reputation: 568
if you want a search with suggestions you can use jquery autocomplete input and link it a view that generate the groceries, no need to use rest-framework for this simple task.
html code:
<script>
$(function() {
$("#your_input_id").autocomplete({
source: "{% url 'url_name' %}",
minLength: 2,
});
});
</script>
lets create the view:
import json # or simplejson
def get_grocery(request):
if request.is_ajax():
q = request.GET.get('term', '')
places = Grocery.objects.filter(grocery_name__icontains=q)
results = []
for pl in places:
place_json = {}
place_json['id'] = pl.id
place_json['label'] = pl.grocery_name
place_json['value'] = pl.grocery_name
results.append(place_json)
data = json.dumps(results)
else:
data = 'fail'
mimetype = 'application/json'
return HttpResponse(data, mimetype)
finaly the url:
url(r'^api/get_grocery/', views.get_grocery, name='url_name'),
don't forget to import the jquery
<!-- jQuery !-->
<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.18/themes/base/jquery-ui.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">
</script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
Upvotes: 1