如何使用迭代器在 django 中构建复杂的查询?实际上,我觉得这可能比 django 更像是“基本的 python”,但这让我很难过。
本质上,我想获取一个列表对象并生成与之等效的对象:
SELECT FROM examples WHERE mystuff LIKE 'EX1' OR mystuff LIKE 'EX2';
这是我尝试的一些示例代码。
from example.models import Examples
from django.db.models import Q
includes = ['EX1', 'EX2']
conditions = []
# I can do something like this, which does build a list of objects
for ex in excludes:
conditions.append(~Q(fullblock_icontains='%s' % ex))
# print conditions
# [<django.db.models.query_utils.Q object at 0x03557AB0>,
# <django.db.models.query_utils.Q object at 0x03557AD0>]
# then I need to do something like this, but obviously this won't work.
blocks = Examples.objects.filter('|'.split(conditions))
另外,我想做同样的事情,但消极(~Q)。我想一旦我们计算出 LIKE,NOT LIKE 就会变得明显。