If I have the next code:
class A(models.Model):
.....
class B(models.Model):
a = models.ManyToManyField(A)
The next queries get differents results:
B.objects.exclude(a__in=[7])
from django.db.models import Q
B.objects.exclude(Q(a__in=[7]))
Results:
- The first query get all objects excluding the "b objects" with a=7. It's Ok
- But the second query get all objects excluding the "b objects" with a=7 or a=None.
Is it an error?, Is it known?
I add a verbose example, execute the next code
from django.contrib.auth.models import User, Group
u1 = User.objects.create(username='u1')
u2 = User.objects.create(username='u2')
u3 = User.objects.create(username='u3')
g1 = Group.objects.create(name='g1')
g2 = Group.objects.create(name='g2')
u1.groups.add(g1)
u2.groups.add(g2)
print User.objects.exclude(groups__in=[g1.pk])
print User.objects.exclude(Q(groups__in=[g1.pk]))
Now this is fixed in Django:
https://code.djangoproject.com/ticket/17600
This was a django bug