Django ORM进行group by 操作
今天在使用django的orm进行group by操作的时候遇到一点小问题,特此记录一下。
model的定义如下:
1 | class TestModel(models.Model): |
设想的sql:
1 | select customer_line_id, count(customer_line_id) as count from t_table group by customer_line_id |
设想的orm语句
1 | TestModel.objects.values('customer_line_id').annotate(count=Count('customer_line_id')) |
django实际执行的sql:
1 | select customer_line_id, count(customer_line_id) as count from t_table group by id order by id desc |
实际并没有如期的按照customer_line_id进行group by。经过查找,是默认的ordering动作产生的影响,可以通过将orderby置空解决
1 | TestModel.objects.values('customer_line_id').annotate(count=Count('customer_line_id')).order_by() |
这样就可以按照预想的情况group by指定字段了。