将基本的SQL查询转换为django orm

发布于 2025-02-11 12:03:59 字数 786 浏览 2 评论 0 原文

我想在表中显示课程名称以及问题数。需要帮助将以下查询转换为django orm:

SELECT DISTINCT exam_course.course_name,
                COUNT(exam_question.question)
FROM exam_course
INNER JOIN exam_question ON exam_question.course_id = exam_course.id
GROUP BY exam_question.course_id



”

”

“在此处输入图像说明”

I want to display the course name along with the question count in a table. Need help to convert below query to a django ORM:

SELECT DISTINCT exam_course.course_name,
                COUNT(exam_question.question)
FROM exam_course
INNER JOIN exam_question ON exam_question.course_id = exam_course.id
GROUP BY exam_question.course_id



Views.Py

Models.py

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2025-02-18 12:03:59

使用注释与注释的注释...以这种方式使用,相应地替换您的要求:

invoices = Invoice.objects.annotate(total_amount=Sum('order__order_items__amount'),number_of_invoices=Count('pk', distinct=True))

Use annotate with count as commented...use in this manner, replace accordingly your requirements:

invoices = Invoice.objects.annotate(total_amount=Sum('order__order_items__amount'),number_of_invoices=Count('pk', distinct=True))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文