I have the following 3 models:
Category:
date_start
date_end
active: bool
Player:
name: str
age: int
category = models.ForeignKey(Category)
PlayerContact:
contact_result: int
player = models.ForeignKey(Player)
In this case I have:
- 2 Categories
- 10 Players per Category
- 1 to 3 Players in each Category with a
contact_result = 3
How do I annotate a Category queryset to get the amount of players with a contact_result=3
?
I've tried this:
Categories.objects.annotate(
Count(
'player',
filter=Q(player__playercontact__contact_result=3)
)
) # returns all players for each Category
Categories.objects.annotate(
Count('player__playercontact__contact_result')
) # returns players with a contact_result but it's not filtered
Something similar to this:
<CategoryQuerySet [<Category: Category object>, <Category: Category object>]>
# where each Category object is annotated by the count() of Players with,
# a PlayerContact's contact_result = 3