While hunting a bug, I found out that the following 2 statements do different things:
Query 1
Order.objects
.filter(items__name__icontains="Foo")
.filter(items__name__icontains="Bar")
.distinct()
Query 2
Order.objects
.filter(
Q(items__name__icontains="Foo") &
Q(items__name__icontains="Bar")
)
.distinct()
The result is as follows:
- Query 1 does include orders that have items which either contain "Foo" or "Bar". For example one item's name is "Foo" while another item's name is "Bar".
- Query 2 however only includes orders that have at least one item that contains all keywords, for example an item with a name of "Foo Bar".
Looking at the queries, I can see that the filter() method adds another INNER JOIN to the query while the other doesn't.
I can see the reasoning behind this, but I really wonder if that's the intended behavior.
