I'm using Django 1.8.
I have a queryset that requires a logical "or" and an "and". It gives :
MyModel.objects.filter( Q(start__gt=today) | Q(end__lte=today), active=True).update(active=False)
As you may understand it, it should take every active MyModel instances which shouldn't have already started, and the one that have finished, and deactivate them.
"start" and "end" are DateFields, and "active" is a boolean.
It works, but it generates a query that is far from being optimised. I'd like to be able to start the query by filtering on "active" state, then check the other two fields, because in my database, I have thousands of entries, but only few of them have active=True. I would say that this boolean test is faster than the comparisons.
I can't reorder the arguments, because the former with the two Q() is a positioned argument while the latter is a name argument, and I can't chain multiple filter() because it generates an "or", and not an "and".
Is there a way to do this?