多对多关系通过 through 显性指定 中间表

通过 类名_set 反向查询

related_name 与 related_query_name 比较

https://www.cnblogs.com/zhufanyu/p/12361607.html

什么时候queryset 才被执行

  • 迭代
  • 切片
  • 序列化
  • list()
  • len()
  • bool()
  • repr()

filter(A,B) 与 filter(A).filter(B) 的区别

通过F ,与同一个表其他字段比较

aggregate 与 annotate区别

select_related()

  1. # 创建数据模型
  2. class Students(models.Model):
  3. GENDER = (
  4. ('1', '男'),
  5. ('2', '女')
  6. )
  7. s_id = models.CharField(max_length=10, verbose_name="学号", primary_key=True)
  8. name = models.CharField(max_length=100, verbose_name="姓名")
  9. csrq = models.CharField(max_length=20, verbose_name="出生日期")
  10. gender = models.CharField(choices=GENDER, verbose_name="性别", max_length=2)
  11. class Meta:
  12. db_table = 'student'
  13. def __str__(self):
  14. return self.name
  15. class Score(models.Model):
  16. student = models.ForeignKey(to="Students", on_delete=models.CASCADE, db_constraint=False, to_field="s_id")
  17. course = models.ForeignKey(to="Courses", on_delete=models.CASCADE, db_constraint=False, to_field="c_id")
  18. score = models.IntegerField(verbose_name="成绩")
  19. class Meta:
  20. db_table = 'score'
  21. class Teachers(models.Model):
  22. t_id = models.CharField(max_length=10, verbose_name="教师编号", primary_key=True)
  23. name = models.CharField(max_length=100, verbose_name="教师名", null=True)
  24. class Meta:
  25. db_table = 'teacher'
  26. def __str__(self):
  27. return self.name
  28. class Courses(models.Model):
  29. c_id = models.CharField(max_length=10, verbose_name="课程号", primary_key=True)
  30. name = models.CharField(max_length=100, verbose_name="课程名称")
  31. teacher = models.ForeignKey(to="Teachers", on_delete=models.CASCADE, db_constraint=False)
  32. student = models.ManyToManyField(to="Students", through="Score")
  33. class Meta:
  34. db_table = 'course'
  35. def __str__(self):
  36. return self.name
  37. # 创建表数据
  38. INSERT INTO "student" VALUES ('0001', '猴子', '1989-01-01', '1');
  39. INSERT INTO "student" VALUES ('0002', '猴子', '1990-12-21', '2');
  40. INSERT INTO "student" VALUES ('0003', '马云', '1991-12-21', '1');
  41. INSERT INTO "student" VALUES ('0004', '王思聪', '1990-05-20', '1');
  42. INSERT INTO "score" VALUES ('1', '80', '0001', '0001');
  43. INSERT INTO "score" VALUES ('2', '90', '0002', '0001');
  44. INSERT INTO "score" VALUES ('3', '99', '0003', '0001');
  45. INSERT INTO "score" VALUES ('4', '60', '0002', '0002');
  46. INSERT INTO "score" VALUES ('5', '80', '0003', '0002');
  47. INSERT INTO "score" VALUES ('6', '80', '0001', '0003');
  48. INSERT INTO "score" VALUES ('7', '80', '0002', '0003');
  49. INSERT INTO "score" VALUES ('8', '80', '0003', '0003');
  50. INSERT INTO "teacher" VALUES ('0001', '孟扎扎');
  51. INSERT INTO "teacher" VALUES ('0002', '马化腾');
  52. INSERT INTO "teacher" VALUES ('0003', NULL);
  53. INSERT INTO "teacher" VALUES ('0004', '');
  54. INSERT INTO "course" VALUES ('0001', '语文', '0002');
  55. INSERT INTO "course" VALUES ('0002', '数学', '0001');
  56. INSERT INTO "course" VALUES ('0003', '英语', '0003');
  57. # 执行案列
  58. # 查询姓“猴”的学生名单
  59. Students.objects.filter(name__contains='猴').values('s_id', 'name', 'csrq')
  60. # 查询姓“孟”老师的个数
  61. Teachers.objects.filter(name__startswith="孟").count()
  62. # 查询课程编号为“0002”的总成绩
  63. Score.objects.filter(course_id='0002').aggregate(s=Sum('score'))
  64. # 查询选了课程的学生人数
  65. Score.objects.values('student_id').distinct().count()
  66. # 查询各科成绩最高和最低的分, 以如下的形式显示:课程号,最高分,最低分
  67. data = Score.objects.values('course_id').annotate(max_score=Max('score'), min_score=Min('score'))
  68. # 查询每门课程被选修的学生数
  69. Score.objects.values('course_id').annotate(student_cn=Count('student_id'))
  70. # 查询男生、女生人数
  71. Students.objects.values('gender').annotate(gender_cn=Count('gender'))
  72. # 查询平均成绩大于60分学生的学号和平均成绩
  73. Score.objects.values('student_id').annotate(avg_score=Avg('score')).filter(avg_score__gte=60)
  74. # 查询至少选修两门课程的学生学号
  75. Score.objects.values('student_id').annotate(course_cn=Count('course_id', distinct=True)).filter(course_cn__gt=2).values('student_id')
  76. # 检索课程编号为“0004”且分数小于60的学生学号,结果按按分数降序排列
  77. Score.objects.filter(course_id='0004', score__lt=60).values('student_id').order_by('-score')
  78. # 查询学生的总成绩并进行排名
  79. Score.objects.values('student_id').annotate(score_sum=Sum('score')).order_by('-score_sum')
  80. # 查询所有课程成绩小于60分学生的学号、姓名
  81. Score.objects.filter(score__lt=60).values('student__s_id', 'student__name')
  82. # 查询所有学生的学号、姓名、选课数、总成绩
  83. Score.objects.values('student').annotate(course_cn=Count('course', distinct=True), score_sum=Sum('score')).
  84. values('student__s_id', 'student__name', 'course_cn', 'score_sum')
  85. # 查询选修“孟扎扎”老师所授课程的学生中成绩最高的学生姓名及其成绩
  86. data = Score.objects.filter(course__c_id__in=Courses.objects.filter(teacher__name='孟扎扎').values('c_id'))
  87. .values('course_id').annotate(score_max=Max('score'))