如何在** kwargs的过滤数据中使用OR运算符?

时间:2012-02-08 10:24:42

标签: django django-models

我有这个:

excl = dict()

kwargs['field1__isnull'] = False
kwargs['field2__isnull'] = False
kwargs['field3__isnull'] = False

items = MyModel.objects.filter(**kwargs).exclude(**excl)

它会生成这个SQL:

SELECT ... FROM `mytable` WHERE (`field1` IS NULL  AND `field2` IS NULL AND `field2` IS NULL)

但我想要这个:

SELECT ... FROM `mytable` WHERE (`field1` IS NULL  AND `field2` IS NULL OR `field2` IS NULL)

怎么做(我必须使用kwargs字典,除非它可以与Q结合使用)?

1 个答案:

答案 0 :(得分:1)

假设danihp对他的问题是正确的,我认为你想要这样的事情:

excl = dict()

f = Q(field1__isnull=False) | Q(field2__isnull=False) | Q(field3__isnull=False)

items = MyModel.objects.filter(f).exclude(**excl)