django rest api用于提前搜索 [英] django rest api for advance search

查看:80
本文介绍了django rest api用于提前搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用django rest框架创建一个api,它将进行一些高级搜索,例如将杂货项目列表保存在数据库中,并且用户要创建杂货列表,然后输入前2或3个字母然后我的api会查询以获取项目名称的其余部分,并向用户建议

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

我已经看到了 drf干草堆,但不确定是否能满足我的要求。它还不支持Django LTS版本1.11。

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 .

能否请您提出一些建议? django rest框架本身是否提供创建这种api的任何支持,该api会进行我上面提到的这种高级搜索?我只是需要一些建议,因为我是django rest框架中的新手。

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.

推荐答案

如果您想使用建议进行搜索,则可以使用jquery自动完成输入并将其链接到生成杂货的视图,无需使用其余框架即可完成此简单任务。

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代码:

<script>
$(function() {
   $("#your_input_id").autocomplete({
   source: "{% url 'url_name' %}",
   minLength: 2,
});
});

</script>

让我们创建视图:

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)

最后输入网址:

url(r'^api/get_grocery/', views.get_grocery, name='url_name'),




不要忘记导入jqu ery

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>

这篇关于django rest api用于提前搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆