filter
在filter之后的是会在过滤后的结果上进行聚合操作,在annotate之后也是可以进行filter的。
Book.objects.filter(name__startswith="Django").annotate(Count('author')
Book.objects.filter(name__startswith="Django").aggregate(Avg('price'))
Book.objects.annotate(num_authors=Count('authors')).filter(num_authors__gt=1)
order by
annotation可以作为排序的依据
values
之前所述,annotation是给匹配到的每行产生一个值
当使用values时,则是会按照给定values字段组合,annotation是对应group的。
group by
注意,values在annotate之前才能完成该操作,否则还是在产生了annotation之后再拿到values。
网友评论