多对多关系通过 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()
# 创建数据模型
class Students(models.Model):
GENDER = (
('1', '男'),
('2', '女')
)
s_id = models.CharField(max_length=10, verbose_name="学号", primary_key=True)
name = models.CharField(max_length=100, verbose_name="姓名")
csrq = models.CharField(max_length=20, verbose_name="出生日期")
gender = models.CharField(choices=GENDER, verbose_name="性别", max_length=2)
class Meta:
db_table = 'student'
def __str__(self):
return self.name
class Score(models.Model):
student = models.ForeignKey(to="Students", on_delete=models.CASCADE, db_constraint=False, to_field="s_id")
course = models.ForeignKey(to="Courses", on_delete=models.CASCADE, db_constraint=False, to_field="c_id")
score = models.IntegerField(verbose_name="成绩")
class Meta:
db_table = 'score'
class Teachers(models.Model):
t_id = models.CharField(max_length=10, verbose_name="教师编号", primary_key=True)
name = models.CharField(max_length=100, verbose_name="教师名", null=True)
class Meta:
db_table = 'teacher'
def __str__(self):
return self.name
class Courses(models.Model):
c_id = models.CharField(max_length=10, verbose_name="课程号", primary_key=True)
name = models.CharField(max_length=100, verbose_name="课程名称")
teacher = models.ForeignKey(to="Teachers", on_delete=models.CASCADE, db_constraint=False)
student = models.ManyToManyField(to="Students", through="Score")
class Meta:
db_table = 'course'
def __str__(self):
return self.name
# 创建表数据
INSERT INTO "student" VALUES ('0001', '猴子', '1989-01-01', '1');
INSERT INTO "student" VALUES ('0002', '猴子', '1990-12-21', '2');
INSERT INTO "student" VALUES ('0003', '马云', '1991-12-21', '1');
INSERT INTO "student" VALUES ('0004', '王思聪', '1990-05-20', '1');
INSERT INTO "score" VALUES ('1', '80', '0001', '0001');
INSERT INTO "score" VALUES ('2', '90', '0002', '0001');
INSERT INTO "score" VALUES ('3', '99', '0003', '0001');
INSERT INTO "score" VALUES ('4', '60', '0002', '0002');
INSERT INTO "score" VALUES ('5', '80', '0003', '0002');
INSERT INTO "score" VALUES ('6', '80', '0001', '0003');
INSERT INTO "score" VALUES ('7', '80', '0002', '0003');
INSERT INTO "score" VALUES ('8', '80', '0003', '0003');
INSERT INTO "teacher" VALUES ('0001', '孟扎扎');
INSERT INTO "teacher" VALUES ('0002', '马化腾');
INSERT INTO "teacher" VALUES ('0003', NULL);
INSERT INTO "teacher" VALUES ('0004', '');
INSERT INTO "course" VALUES ('0001', '语文', '0002');
INSERT INTO "course" VALUES ('0002', '数学', '0001');
INSERT INTO "course" VALUES ('0003', '英语', '0003');
# 执行案列
# 查询姓“猴”的学生名单
Students.objects.filter(name__contains='猴').values('s_id', 'name', 'csrq')
# 查询姓“孟”老师的个数
Teachers.objects.filter(name__startswith="孟").count()
# 查询课程编号为“0002”的总成绩
Score.objects.filter(course_id='0002').aggregate(s=Sum('score'))
# 查询选了课程的学生人数
Score.objects.values('student_id').distinct().count()
# 查询各科成绩最高和最低的分, 以如下的形式显示:课程号,最高分,最低分
data = Score.objects.values('course_id').annotate(max_score=Max('score'), min_score=Min('score'))
# 查询每门课程被选修的学生数
Score.objects.values('course_id').annotate(student_cn=Count('student_id'))
# 查询男生、女生人数
Students.objects.values('gender').annotate(gender_cn=Count('gender'))
# 查询平均成绩大于60分学生的学号和平均成绩
Score.objects.values('student_id').annotate(avg_score=Avg('score')).filter(avg_score__gte=60)
# 查询至少选修两门课程的学生学号
Score.objects.values('student_id').annotate(course_cn=Count('course_id', distinct=True)).filter(course_cn__gt=2).values('student_id')
# 检索课程编号为“0004”且分数小于60的学生学号,结果按按分数降序排列
Score.objects.filter(course_id='0004', score__lt=60).values('student_id').order_by('-score')
# 查询学生的总成绩并进行排名
Score.objects.values('student_id').annotate(score_sum=Sum('score')).order_by('-score_sum')
# 查询所有课程成绩小于60分学生的学号、姓名
Score.objects.filter(score__lt=60).values('student__s_id', 'student__name')
# 查询所有学生的学号、姓名、选课数、总成绩
Score.objects.values('student').annotate(course_cn=Count('course', distinct=True), score_sum=Sum('score')).
values('student__s_id', 'student__name', 'course_cn', 'score_sum')
# 查询选修“孟扎扎”老师所授课程的学生中成绩最高的学生姓名及其成绩
data = Score.objects.filter(course__c_id__in=Courses.objects.filter(teacher__name='孟扎扎').values('c_id'))
.values('course_id').annotate(score_max=Max('score'))