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?

Was it helpful?

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top