I have a curious problem.
I have 3 objects. All the same
class Articles(models.Model):
owner = models.ForeignKey(Author)
tags = models.ManyToManyField('Tag')
class Tag(models.Model):
name = models.CharField(max_length=255)
and so I have 3 Articles. With all the same tags: 'tag1' and 'tag2'
And I have queries
actionsAll = Articles.objects.filter((Q(tags__name__exact="tag1") | Q(tags__name__exact="tag2"))).distinct()
This gives me all my articles. It will return 6 articles w/o distinct() since it will collect each article 2x since they have both tags.
However with this query:
actionsAll = Articles.objects.filter((Q(tags__name__exact="tag1") & Q(tags__name__exact="tag2"))).distinct()
This gives me no articles. Since the articles contain both the tags, it should return them all shouldnt it?
or
andand
do very different things from|
and&
; don't try to use them as a replacement. – Laundromatand
andor
do not provide the "joining" into one query that&
and|
do; instead they return one of their operands based on their "trueness". It may work for the data you have, but as your data widens you'll find that there are logic errors in it. – Laundromat