[Django]-Django. How Q() really works?

4👍

You should use (Q(remark='credits gained')|Q(remark='credits given')), not(Q(remark='credits gained') or Q(remark='credits given'))

It’s 2 completely different operators:
| is bitwise OR, but it’s overriden for Q():

def __or__(self, other):
    return self._combine(other, self.OR)

while or is logical (or rather ‘coalescing’) operator. It means that Q(remark='credits gained') or Q(remark='credits given') will return first not-None object, that’s why in your second case it will result in Q(remark='credits gained'), and in the third one – in Q(remark='credits given')

Leave a comment