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

后端 未结 2 1361
别那么骄傲
别那么骄傲 2021-01-20 10:37

This should be easy but for some reason I\'m having trouble finding it. I have the following:

App(models.Model):
    ...

Release(models.Model):
    date = m         


        
相关标签:
2条回答
  • 2021-01-20 10:51

    You should be able to use:

    App.objects.annotate(release_count=Count('release')).filter(release_count__gt=0)\
        .order_by('-release__date')
    
    0 讨论(0)
  • 2021-01-20 10:56
    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.

    0 讨论(0)
提交回复
热议问题