See this question and answer here
You basically need to annotate/aggregate and then filter. The order is important.
Filtering after annotating creates the having clause you need
Question
I'm looking to only pull collections where all related tags exist. This is the query that returns what I need. Now I need to figure out a way to get this to work with tastypie.
Currently I can query like this, but It pulls all collections that have those associated tags (even if the collection only has one). I need to pull collections that have both.
/collection/?tag__name__in=Tag1,Tag2
The query that accomplishes what I need is:
select * from collection
join tag
on tag.collection_id = tag.id
where tag.name in ('Tag1', 'Tag2')
group by
collection.name
having
COUNT(DISTINCT tag.name) = 2;
Would build_filters
be the way to go for this?
Solution
See this question and answer here
You basically need to annotate/aggregate and then filter. The order is important.
Filtering after annotating creates the having clause you need