The Python Oracle

Django query to retrieve rows that meet aggregate criteria on related table

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping

--

Chapters
00:00 Question
01:29 Accepted answer (Score 2)
01:55 Thank you

--

Full question
https://stackoverflow.com/questions/2555...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django

#avk47



ACCEPTED ANSWER

Score 2


from django.db.models import Count

Task.objects.exclude(assignments__status='r').annotate(assignments_count=Count('assignments').filter(assignments_count__gt=N)