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

查看:20
本文介绍了仅过滤 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?

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天全站免登陆