简体   繁体   English

将基本 sql 查询转换为 django ORM

[英]Convert a basic sql query to django ORM

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:需要帮助将以下查询转换为 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



视图.Py

模型.py

在此处输入图像描述

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM