Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Django queryset在列中搜索多个单词_Django_Django Queryset - Fatal编程技术网

使用Django queryset在列中搜索多个单词

使用Django queryset在列中搜索多个单词,django,django-queryset,Django,Django Queryset,我有一个自动完成框,需要返回有输入字的结果。然而,输入字可以是局部的,并且位于不同的顺序或位置 示例: 数据库列中的值(MySQL)- 现在,如果用户输入季度销售额,那么它应该返回前两个结果 我试过: column__icontains = term #searches only '%quarter sales% and thus gives no results column__search = term #searches any of complete words and also r

我有一个自动完成框,需要返回有输入字的结果。然而,输入字可以是局部的,并且位于不同的顺序或位置

示例:

数据库列中的值(MySQL)-

现在,如果用户输入
季度销售额
,那么它应该返回前两个结果

我试过:

column__icontains = term  #searches only '%quarter sales% and thus gives no results
column__search = term  #searches any of complete words and also returns last result
**{'ratio_name__icontains':each_term for each_term in term.split()}   #this searches only sales as it is the last keyword argument

由于这是一种常见的模式,所以我在Django中内置了任何通过正则表达式或正则表达式实现的技巧吗?

搜索引擎更适合此任务,但您仍然可以使用基本代码来实现。 如果要查找包含“A”和“B”的字符串,可以


但实际上,你最好使用一个简单的全文搜索引擎,配置很少,比如Haystack/Whoosh

也许我弄错了,但对我来说这更像是一个全文搜索问题。你应该看看MySQL全文搜索,看看它是否适合你的需要。你试过使用任何搜索引擎吗?查看haystack()。它支持一开始很容易使用的嗖嗖声,更重要的是,我尝试过全文搜索,但它返回的结果包含上述任何一个单词。例如,它还返回12个月的销售额(它不应该返回)。如果您不想设置全文搜索引擎,数据库现在内置了全文搜索功能。你也可以。当然,内置的全文搜索功能不如专用搜索引擎好,但它仍然比为每个单词链接过滤器要好得多。
column__icontains = term  #searches only '%quarter sales% and thus gives no results
column__search = term  #searches any of complete words and also returns last result
**{'ratio_name__icontains':each_term for each_term in term.split()}   #this searches only sales as it is the last keyword argument
Model.objects.filter(string__contains='A').filter(string__contains='B')
Model.objects.filter(Q(string__contains='A') & Q(string__contains='B'))