ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 第十一节:ORM作业参考答案 # ORM作业参考答案: 1. 查询平均成绩大于60分的同学的id和平均成绩; ``` <pre class="calibre12">``` rows = Student.objects.annotate(avg=Avg(<span class="hljs-string">"score__number"</span>)).filter(avg__gte=<span class="hljs-params">60</span>).values(<span class="hljs-string">"id"</span>,<span class="hljs-string">"avg"</span>) <span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> rows: print(row) ``` ``` 2. 查询所有同学的id、姓名、选课的数、总成绩; ``` <pre class="calibre12">``` rows = Student.objects.annotate(course_nums=Count(<span class="hljs-string">"score__course"</span>),total_score=Sum(<span class="hljs-string">"score__number"</span>)) .values(<span class="hljs-string">"id"</span>,<span class="hljs-string">"name"</span>,<span class="hljs-string">"course_nums"</span>,<span class="hljs-string">"total_score"</span>) <span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> rows: print(row) ``` ``` 3. 查询姓“李”的老师的个数; ``` <pre class="calibre12">``` teacher_nums = Teacher.objects.filter(name__startswith=<span class="hljs-string">"李"</span>).count() print(teacher_nums) ``` ``` 4. 查询没学过“黄老师”课的同学的id、姓名; ``` <pre class="calibre12">``` rows = Student.objects.exclude(score__course__teacher__name=<span class="hljs-string">"黄老师"</span>).values(<span class="hljs-string">'id'</span>,<span class="hljs-string">'name'</span>) <span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> rows: print(row) ``` ``` 5. 查询学过课程id为1和2的所有同学的id、姓名; ``` <pre class="calibre12">``` rows = Student.objects.filter(score__course__in=[<span class="hljs-params">1</span>,<span class="hljs-params">2</span>]).distinct().values(<span class="hljs-string">'id'</span>,<span class="hljs-string">'name'</span>) <span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> rows: print(row) ``` ``` 6. 查询学过“黄老师”所教的所有课的同学的学号、姓名; ``` <pre class="calibre12">``` rows = Student.objects.annotate(nums=Count(<span class="hljs-string">"score__course"</span>,filter=Q(score__course__teacher__name=<span class="hljs-string">'黄老师'</span>))) .filter(nums=Course.objects.filter(teacher__name=<span class="hljs-string">'黄老师'</span>).count()).values(<span class="hljs-string">'id'</span>,<span class="hljs-string">'name'</span>) <span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> rows: print(row) ``` ``` 7. 查询所有课程成绩小于60分的同学的id和姓名; ``` <pre class="calibre12">``` students = Student.objects.exclude(score__number__gt=<span class="hljs-params">60</span>) <span class="hljs-keyword">for</span> student <span class="hljs-keyword">in</span> students: print(student) ``` ``` 8. 查询没有学全所有课的同学的id、姓名; ``` <pre class="calibre12">``` students = Student.objects.annotate(num=Count(F(<span class="hljs-string">"score__course"</span>))).filter(num__lt=Course.objects.count()).values(<span class="hljs-string">'id'</span>,<span class="hljs-string">'name'</span>) <span class="hljs-keyword">for</span> student <span class="hljs-keyword">in</span> students: print(student) ``` ``` 9. 查询所有学生的姓名、平均分,并且按照平均分从高到低排序; ``` <pre class="calibre12">``` students = Student.objects.annotate(avg=Avg(<span class="hljs-string">"score__number"</span>)).order_by(<span class="hljs-string">"-avg"</span>).values(<span class="hljs-string">'name'</span>,<span class="hljs-string">'avg'</span>) <span class="hljs-keyword">for</span> student <span class="hljs-keyword">in</span> students: print(student) ``` ``` 10. 查询各科成绩的最高和最低分,以如下形式显示:课程ID,课程名称,最高分,最低分: ``` <pre class="calibre12">``` courses = Course.objects.annotate(min=Min(<span class="hljs-string">"score__number"</span>),max=Max(<span class="hljs-string">"score__number"</span>)).values(<span class="hljs-string">"id"</span>,<span class="hljs-string">'name'</span>,<span class="hljs-string">'min'</span>,<span class="hljs-string">'max'</span>) <span class="hljs-keyword">for</span> course <span class="hljs-keyword">in</span> courses: print(course) ``` ``` 11. 查询每门课程的平均成绩,按照平均成绩进行排序; ``` <pre class="calibre12">``` courses = Course.objects.annotate(avg=Avg("score__number")).order_by('avg').values('id','name','avg') for course in courses: print(course) ``` ``` 12. 统计总共有多少女生,多少男生; ``` <pre class="calibre12">``` rows = Student.objects.aggregate(male_num=Count(<span class="hljs-string">"gender"</span>,filter=Q(gender=<span class="hljs-params">1</span>)),female_num=Count(<span class="hljs-string">"gender"</span>,filter=Q(gender=<span class="hljs-params">2</span>))) print(rows) ``` ``` 13. 将“黄老师”的每一门课程都在原来的基础之上加5分; ``` <pre class="calibre12">``` rows = Score.objects.filter(course__teacher__name=<span class="hljs-string">'黄老师'</span>).update(number=F(<span class="hljs-string">"number"</span>)+<span class="hljs-params">5</span>) print(rows) ``` ``` 14. 查询两门以上不及格的同学的id、姓名、以及不及格课程数; ``` <pre class="calibre12">``` students = Student.objects.annotate(bad_count=Count(<span class="hljs-string">"score__number"</span>,filter=Q(score__number__lt=<span class="hljs-params">60</span>))).filter(bad_count__gte=<span class="hljs-params">2</span>).values(<span class="hljs-string">'id'</span>,<span class="hljs-string">'name'</span>,<span class="hljs-string">'bad_count'</span>) <span class="hljs-keyword">for</span> student <span class="hljs-keyword">in</span> students: print(student) ``` ``` 15. 查询每门课的选课人数; ``` <pre class="calibre12">``` courses = Course.objects.annotate(student_nums=Count(<span class="hljs-string">"score__student"</span>)).values(<span class="hljs-string">'id'</span>,<span class="hljs-string">'name'</span>,<span class="hljs-string">'student_nums'</span>) <span class="hljs-keyword">for</span> course <span class="hljs-keyword">in</span> courses: print(course) ``` ```