Mitchell van Zuylen
Mitchell van Zuylen

Reputation: 4115

Django: And conditional in Case

I want to do something like this:

all_objects.annotate(c=Count(
                             Case(
                             When(hit__question=A AND                                 
                                  hit__participant=B, then=1 ) 

How would i go about this?

Upvotes: 2

Views: 45

Answers (1)

mattjegan
mattjegan

Reputation: 2884

The When object can behave just like a normal .filter so we just need to add the different field lookups as separate arguments like so:

all_objects.annotate(c=Count(
                             Case(
                             When(hit__question=A,                                
                                  hit__participant=B, then=1 )
                             # rest of query

Upvotes: 2

Related Questions