[Django]-Django queries: Count number of objects with FK to model instance

4👍

You should be able to use:

App.objects.annotate(release_count=Count('release')).filter(release_count__gt=0)\
    .order_by('-release__date')

2👍

App.objects \
    .annotate(release_count=Count('release')) \
    .(release_count__gt=0) \
    .order_by('-release_count')

For the bonus part, denormalizing date field looks like the only solution at the moment. And it’s pretty fast too.

Leave a comment