参考:点击打开链接
1. group by & order by & Q
聚合+排序,对应的sql语句是:
Select currentpid, count(*) from stats_x
where type=3 and (status =1 or status=0)
group by currentpid
order by currentpid desc
对应的django 写法:
stats_x.objects.filter(Q(type=3) & ( Q(status=1) | Q(status=0) ) ).values('currentpid').annotate(delta_count=Count('id')).order_by('-currentpid')
#order by -号是倒排。
#在annotate前面添加values,即以该属性列表为group by的列
#annotate 与aggregate一样都是可以用于聚合,但是aggregate 聚合后的column 名字是固定的,不能自定义,annotate可以自定义列名,例如delta_count
Note: Q 表达式之间不能用AND ,必须用&
2. TO BE CONTINUED……