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

后端 未结 2 1402
别那么骄傲
别那么骄傲 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')
    

提交回复
热议问题