您的当前位置:首页正文

django Conditional Expression

来源:花图问答

需求:一对多,需要得到多的那边符合某些条件的实例数量。


topic.objects.all().annotate(

    post_count=Sum(

        Case(When(post__is_active=True,then=1),default=0,output_field=IntegerField())))