Python/Django — what is the difference between the “and” operator and the “&” operator

前端 未结 3 1249
[愿得一人]
[愿得一人] 2021-01-24 00:25

I have a interesting Django problem.

Consider the following:

Model.objects.filter(Q(id=\'test1\') and Q(id=\'test2\'))

this returns the

3条回答
  •  庸人自扰
    2021-01-24 00:53

    according to the following tutorial

    & is a Binary AND Operator copies a bit to the result if it exists in both operands.

    Example: Assume if a = 60; and b = 13; Now in binary format they will be as follows:

    a = 0011 1100

    b = 0000 1101

    (a & b) will give 12 which is 0000 1100

    and Called Logical AND operator. If both the operands are true then then condition becomes true.

    Example: (a and b) is true.

    So the & was performing binary addition on your query set. Interesting.

提交回复
热议问题