🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 第十节:ORM作业 # ORM作业: 假设有以下`ORM`模型: ``` <pre class="calibre12">``` <span class="hljs-keyword">from</span> django.db <span class="hljs-keyword">import</span> models <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span><span class="hljs-params">(models.Model)</span>:</span> <span class="hljs-string">"""学生表"""</span> name = models.CharField(max_length=<span class="hljs-params">100</span>) gender = models.SmallIntegerField() <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span> db_table = <span class="hljs-string">'student'</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Course</span><span class="hljs-params">(models.Model)</span>:</span> <span class="hljs-string">"""课程表"""</span> name = models.CharField(max_length=<span class="hljs-params">100</span>) teacher = models.ForeignKey(<span class="hljs-string">"Teacher"</span>,on_delete=models.SET_NULL,null=<span class="hljs-keyword">True</span>) <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span> db_table = <span class="hljs-string">'course'</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Score</span><span class="hljs-params">(models.Model)</span>:</span> <span class="hljs-string">"""分数表"""</span> student = models.ForeignKey(<span class="hljs-string">"Student"</span>,on_delete=models.CASCADE) course = models.ForeignKey(<span class="hljs-string">"Course"</span>,on_delete=models.CASCADE) number = models.FloatField() <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span> db_table = <span class="hljs-string">'score'</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Teacher</span><span class="hljs-params">(models.Model)</span>:</span> <span class="hljs-string">"""老师表"""</span> name = models.CharField(max_length=<span class="hljs-params">100</span>) <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span> db_table = <span class="hljs-string">'teacher'</span> ``` ``` 使用之前学到过的操作实现下面的查询操作: 1. 查询平均成绩大于60分的同学的id和平均成绩; 2. 查询所有同学的id、姓名、选课的数量、总成绩; 3. 查询姓“李”的老师的个数; 4. 查询没学过“李老师”课的同学的id、姓名; 5. 查询学过课程id为1和2的所有同学的id、姓名; 6. 查询学过“黄老师”所教的“所有课”的同学的id、姓名; 7. 查询所有课程成绩小于60分的同学的id和姓名; 8. 查询没有学全所有课的同学的id、姓名; 9. 查询所有学生的姓名、平均分,并且按照平均分从高到低排序; 10. 查询各科成绩的最高和最低分,以如下形式显示:课程ID,课程名称,最高分,最低分; 11. 查询没门课程的平均成绩,按照平均成绩进行排序; 12. 统计总共有多少女生,多少男生; 13. 将“黄老师”的每一门课程都在原来的基础之上加5分; 14. 查询两门以上不及格的同学的id、姓名、以及不及格课程数; 15. 查询每门课的选课人数;