2👍
✅
You can just chain Q
objects with the logical or operator |
. For example:
cond = Q(col1=val1)
if some_condition:
cond |= Q(col2=val2)
q = MyModel.objects.filter(cond)
1👍
You can also use the union() QuerySet method.
q1 = MyModel.objects.filter(cond1)
q2 = MyModel.objects.filter(cond2)
q3 = MyModel.objects.filter(cond3)
q2.union(q1)
q3.union(q1)
Source:stackexchange.com