Django分组聚合的坑
用如下语句Group By并算出每个类别的总和, 总是得不出要用的结果, 也就是分组总不成功

queryset = MyModel.objects.all().values('category').annotate(total_amount=Sum('amount'))

打印Sql语句之, print(queryset.query)

发现Group by 条件不只是把category列进去了, 还把另外一个字段A也列了进去, 原来是是模型里用了字段A默认排序, 给自动加到这来了.

清空默认排序, 改成如下语句即可:

queryset = MyModel.objects.all().values('category').order_by().annotate(total_amount=Sum('amount'))
For example, "name@something.com". If someone replies to you it will be via email.
For example, "http://someaddress.com"