使用 raw 查询,提示没有主键,InvalidQuery('Raw query must include the primary key'),openid 非主键
answer_num = cls.objects.raw("SELECT COUNT(*) FROM server_answerlog WHERE `paper_id` = %d GROUP BY `openid`" % int(paper_id))
使用
answer_num = cls.objects.filter(paper_id=paper_id).values('openid').annotate(num=Count('openid'))
生成出来了并没有我想要的 group by openid
SELECT `server_answerlog`.`openid`, COUNT(`server_answerlog`.`openid`) AS `num` FROM `server_answerlog` WHERE `server_answerlog`.`paper_id` = 1 GROUP BY `server_answerlog`.`id` ORDER BY `server_answerlog`.`id` ASC
对 annotate 不太熟悉,特来求助各位大佬
1
banbo 2018-05-02 14:52:49 +08:00
Count(主键)
|