使用Q对象时排除vs过滤器 [英] Exclude vs filter, when using Q objects

查看:41
本文介绍了使用Q对象时排除vs过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道怎么可能:

A = object_list.filter( Q(sales__sale_starts__lte=today) & Q(sales__sale_ends__gte=today) )
# query inside filter catches 2 objects

B = object_list.exclude( Q(sales__sale_starts__lte=today) & Q(sales__sale_ends__gte=today) )
# query inside exclude catches 3 objects,
# though it is the same as previous

# in other words: object_list contains 20 objects,
# A - 2 objects, and B - 17 objects

使用 Q 对象时, filter() exclude()的工作方式是否有区别?谢谢.

Is there any difference in how filter() and exclude() working when using Q objects? Thanks.

我希望 B 与以下内容相同:

I expect B to be as in:

B = object_list.difference(A)
# B contains 18 objects

推荐答案

如@PauloAlmeida所说

As @PauloAlmeida said

多值结果可能是问题所在,请参见以下两个错误报告:排除具有多个条件的查询对于相同的多值关系不正确; 否定Q对象的问题.

由于不同的sql生成,因此 filter() exclude()之间确实存在差异.也许是 exclude()中的错误.

So there is indeed can be a difference between filter() and exclude(), because of different sql generation. And maybe bugs in exclude().

这篇关于使用Q对象时排除vs过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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