需求:一對多宙址,需要得到多的那邊符合某些條件的實例數(shù)量奖年。
topic.objects.all().annotate(
post_count=Sum(
Case(When(post__is_active=True,then=1),default=0,output_field=IntegerField())))
參考文檔:https://docs.djangoproject.com/en/1.10/ref/models/conditional-expressions/