Django组合了两个没有Q的查询。

时间:2021-01-15 01:12:17

This is the problem I am currently having:

这就是我目前所面临的问题:

I filter out models using Q() and get x results. I then examine x results to determine certain conditions. Depending on those condition, I perform another query and get y results. It's at this point that I would like to combine x results and y results and then perform a filter on both. Keep in mind that both queries are of the same model.

我使用Q()过滤模型并得到x结果。然后我检查x结果以确定某些条件。根据这些条件,我执行另一个查询并得到y结果。在这一点上,我想把x的结果和y的结果结合起来,然后在两者上执行一个过滤器。请记住,这两个查询都是同一个模型。

Since I cannot perform query 2 until I have results from Query 1, it's not possible to combine these 2 queries using Q().

因为我无法执行查询2,直到查询1的结果,所以不可能使用Q()组合这两个查询。

Is there some sort of way that I can combine the results together? Basically what I am trying to achieve would sort of look like this:

有没有什么方法可以让我把这些结果结合起来?基本上我想要达到的效果是这样的:

all_results = x_results.extend(y_results)
all_results.filter(price = 500)

1 个解决方案

#1


3  

I think you can do something like this

我认为你可以做这样的事情。

all_results = x_results | y_results

#1


3  

I think you can do something like this

我认为你可以做这样的事情。

all_results = x_results | y_results