Q objects and the '&' operator in django Q objects and the '&' operator in django django django

Q objects and the '&' operator in django


If you look at the SQL it generates, you'll see that it checks to see if the same tag has both names. What you need is an IN query or an EXISTS query that traverses the relation.


** import Q from djangofrom *models import SuperUser, NameUserimport operator# we do not know the name in the superherosuper_users = SuperUser.objects.all()q_expressions = [Q(username=user.username) for user in super_users]# we have bind super_hero with username_superheroes_qs = models.NameUser.objects.filter(reduce(operator.or_, q_expressions))