Filtering only on Annotations in Django

后端 未结 6 2064
执念已碎
执念已碎 2020-12-13 14:13

Taking the example from: http://docs.djangoproject.com/en/dev/topics/db/aggregation/#filter-and-exclude

Publisher.objects.filter(book__rating__gt=3.0).annota         


        
6条回答
  •  星月不相逢
    2020-12-13 14:45

    from django.db import models
    
    Publisher.objects.annotate(
        num_books=models.Sum(
            models.Case(
                models.When(
                    book__rating__gt=3.0,
                    then=1,
                ),
                default=0,
                output_field=models.IntegerField(),
            )
        )
    ).filter(
        num_books=0,
    )
    

提交回复
热议问题