繁体   English   中英

Django Haystack。 和,或在搜索查询集中

[英]Django Haystack. And, or in search queryset

使用:干草堆和索尔。

我需要创建一个搜索查询集以按过滤条件搜索产品。

首先,我只需要过滤基于我的网站(Django网站框架)的产品。 所以我做:

sqs = sqs.filter(site=site.pk)

它返回这样的搜索查询:

site:(6)

好。

然后我需要按属性过滤:

sqs = sqs.filter(attribute_codes='power', attribute_values__range=(20, 30))
sqs = sqs.filter(attribute_codes='power', attribute_values__range=(40, 50))

并生成这样的查询:

(site:(6) AND attribute_codes:(power) AND attribute_values:(["20" TO "30"]) AND attribute_values:(["40" TO "50"]))

但是,我需要进行如下查询:

(site=6) AND ((attributes1) OR (attributes2))

所以我尝试filter_or属性的过滤更改为filter_or

 sqs = sqs.filter_or(attribute_codes='power', attribute_values__range=(20, 30))
sqs = sqs.filter_or(attribute_codes='power', attribute_values__range=(40, 50))

结果是:

(site:(6) OR (attribute_codes:(power) AND attribute_values:(["20" TO "30"])) OR (attribute_codes:(power) AND attribute_values:(["40" TO "50"])))

但是我还需要:

    (site=6) AND ((attributes1) OR (attributes2))

那么,该怎么做呢? 请帮帮我

像Django的queryset Q对象一样,django haystack也有一个SQ对象,该对象允许使用| &运算符

sqs = sqs.filter(site=6)
sqs = sqs.filter(SQ(attribute_codes='power') | SQ(attribute_values__range=(20, 30))

要么

sqs = sqs.filter(
    SQ(site=6) & (SQ(attribute_codes='power') | SQ(attribute_values__range=(20, 30))
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM