仅在Django中的注释上进行过滤 [英] Filtering only on Annotations in Django

查看:110
本文介绍了仅在Django中的注释上进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下列示例:
http://docs.djangoproject.com/en/dev/topics/db/aggregation/#filter-and-exclude

  Publisher.objects.filter(book__rating__gt = 3.0).annotate(num_books = Count('book'))

是否有过滤器仅适用于注释,所以它会返回所有发布者,有些具有num_books = 0?

解决方案

您可以在过滤器中使用注释变量。

  publishers = Publisher.objects.annotate num_books = Count('book'))filter(num_books__gte = 2)


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

Publisher.objects.filter(book__rating__gt=3.0).annotate(num_books=Count('book'))

Is there anyway to have the filter only apply to the annotation, so it would return all publishers, with some having a num_books=0?

解决方案

You can use the annotation variable in the filter.

publishers=Publisher.objects.annotate(num_books=Count('book')).filter(num_books__gte=2)

这篇关于仅在Django中的注释上进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆