The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Flying Over Ancient Lands

--

Chapters
00:00 Django Query To Retrieve Rows That Meet Aggregate Criteria On Related Table
01:09 Accepted Answer Score 2
01:29 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)