今天需要在django上实现group by但是网上的例子在我的电脑上怎么都试不出来
例子:
sql语句
select name,count(id) as counts from foo_personmodel group by name;
django实现语句
PersonModel.objects.values("name").annotate(counts=Count(id))
虽然语句是对的但是一直报错NameError: name 'Count' is not defined
然后才发现是少了一个包
在import部分输入from django.db.models.aggregates import Count即可



