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?
Q object encapsulates a SQL expression in a Python object that can be used in database-related operations. Using Q objects we can make complex queries with less and simple code. For example, this statement returns if the question starts with 'who' or with 'what'.
This is because a Django QuerySet is a lazy object. It contains all of the information it needs to populate itself from the database, but will not actually do so until the information is needed.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With