You can add Q objects together, using their add
method. For example:
>>> q = Q(sender=x)
>>> q.add(Q(receiver=y), Q.AND)
The second argument to add
is the connector, which can also be Q.OR
EDIT: My answer is merely a different way of doing what Perrin Harkins suggested, but regarding your other concern, about different behavior of filter
depending on the way you construct the query, you don't have to worry about that if you join Q objects. My example is equivalent to filter(sender=x, receiver=y)
, and not filter(sender=x).filter(receiver=y)
, because Q objects, as far as I could see in a quick test, do an immediate AND on the clauses and don't have the special behavior of filter
for multi-valued relations.
In any case, nothing like looking at the SQL and making sure it really is doing the same in your specific queries.