Django的orm練習

models(建立表):spa

class Grade(models.Model):
    """年級表"""
    gname=models.CharField(max_length=32)


class Classes(models.Model):
    """班級表"""
    caption=models.CharField(max_length=32)
    grade=models.ForeignKey("Grade", on_delete=models.CASCADE)
    teach_cls = models.ManyToManyField('Teacher')


class Teacher(models.Model):
    """老師表"""
    tname=models.CharField(max_length=32)


class Course(models.Model):
    """課程表"""
    cname=models.CharField(max_length=32)
    teacher=models.OneToOneField("Teacher", on_delete=models.CASCADE)


class Student(models.Model):
    """學生表"""
    sname = models.CharField(max_length=32)
    gender = models.CharField(max_length=10)
    classes = models.ForeignKey("Classes",on_delete=models.CASCADE)


class Score(models.Model):
    """成績表"""
    student = models.ForeignKey("Student",on_delete=models.CASCADE)
    course = models.ForeignKey("Course",on_delete=models.CASCADE)
    score=models.IntegerField()

插入數據:code

def orm_insert(request):
    """orm表記錄插入"""

    # 教師表插入
    t1 = Teacher.objects.create(tname="張三")
    t2 = Teacher.objects.create(tname="李四")
    t3 = Teacher.objects.create(tname="王五")

    # 年級表插入
    g1 = Grade.objects.create(gname="一年級")
    g2 = Grade.objects.create(gname="二年級")
    g3 = Grade.objects.create(gname="三年級")

    # 課程表插入
    c1 = Course.objects.create(cname="生物",teacher_id=1)
    c2 = Course.objects.create(cname="體育",teacher_id=2)
    c3 = Course.objects.create(cname="物理",teacher_id=3)


    # 班級表
    cl1 = Classes.objects.create(caption="一年一班",grade_id=1)
    cl11 = Classes.objects.create(caption="一年二班",grade_id=1)
    cl2 = Classes.objects.create(caption="二年一班",grade_id=2)
    cl22 = Classes.objects.create(caption="二年二班",grade_id=2)
    cl3 = Classes.objects.create(caption="三年一班",grade_id=3)
    cl33 = Classes.objects.create(caption="三年二班",grade_id=3)
    cl1.teach_cls.add(t1,t2,t3)
    cl11.teach_cls.add(t1)
    cl2.teach_cls.add(t2,t3)
    cl22.teach_cls.add(t1,t3)
    cl3.teach_cls.add(t3)
    cl33.teach_cls.add(t1,t2,t3)


    # 學生表
    s1 = Student.objects.create(sname="劉龍康",gender='',classes_id=1)
    s2 = Student.objects.create(sname="小胖",gender='',classes_id=1)
    s3 = Student.objects.create(sname="小明",gender='',classes_id=2)
    s4 = Student.objects.create(sname="小紅",gender='',classes_id=3)
    s5 = Student.objects.create(sname="小花",gender='',classes_id=4)
    s6 = Student.objects.create(sname="劉能",gender='',classes_id=5)
    s7 = Student.objects.create(sname="胖妞",gender='',classes_id=1)

    # 成績表
    sc1 = Score.objects.create(student=s1,course=c1,score=60)
    sc2 = Score.objects.create(student=s1,course=c2,score=80)
    sc3 = Score.objects.create(student=s1,course=c3,score=90)
    sc4 = Score.objects.create(student=s2,course=c1,score=60)
    sc5 = Score.objects.create(student=s2,course=c2,score=70)
    sc6 = Score.objects.create(student=s7,course=c2,score=100)
    sc7 = Score.objects.create(student=s3,course=c3,score=30)
    sc8 = Score.objects.create(student=s4,course=c1,score=20)
    sc9 = Score.objects.create(student=s5,course=c1,score=60)
    sc10 = Score.objects.create(student=s6,course=c1,score=80)
    sc11 = Score.objects.create(student=s7,course=c1,score=20)
    return HttpResponse("ok")

查詢練習:orm

def orm_search(request):
    """orm做業查詢"""
    # 1.查詢學生總人數;
    # ret = Student.objects.aggregate(student_count=Count('id'))
    # ret = Student.objects.count()

    # 2.查詢生物課程和物理課程成績都及格的學生id和姓名
    # ret = Student.objects.annotate(jgs=Count('id')).filter(score__course__cname__in=['生物','物理'],score__score__gte=60,jgs=2).values("id","sname")

    # 3.查詢每一個年級的班級數,取出班級數最多的前二個年級
    # ret = Grade.objects.annotate(cls_count=Count('classes__grade')).order_by('-cls_count').values('gname','cls_count')

    # 4.查詢平均成績最高的學生的id和姓名以及平均成績
    # ret = Student.objects.annotate(avg_score=Avg('score__score')).order_by('-avg_score').values('id','sname','avg_score')[0]

    # 5.查詢每一個年級的學生人數
    # ret = Grade.objects.annotate(stu_count=Count('classes__student__id')).values('gname','stu_count')

    # 6.查詢每位學生的學號,姓名,平均成績
    # ret = Student.objects.annotate(stu_avg=Avg('score__score')).values('id','sname','stu_avg')

    # 7.查詢學生編號爲2的學生姓名,該學生成績最高的課程名及分數
    # ret = Student.objects.filter(id=2).values('sname','score__course__cname','score__score').order_by('-score__score')[0]
    # ret = Student.objects.filter(id="2").annotate(scoreMax=Max("score__score")).order_by('-scoreMax')[0:1].values("sname", "score__course__cname", "scoreMax")

    # 8.查詢姓李的老師的個數和所帶班級數
    # ret = Teacher.objects.filter(tname__startswith='李').annotate(cls_count=Count('classes')).values('tname','cls_count')

    # 9.查詢班級數小於3的年級id和年級名
    # ret = Grade.objects.annotate(cls_count=Count('classes')).values('id','gname','cls_count').filter(cls_count__lt=3)

    # 10.查詢教過課程超過2門的老師的id和姓名
    # ret = Teacher.objects.annotate(cour_count=Count('course')).filter(cour_count__gte=1).values('id','tname','cour_count')

    # 11.查詢教過超過3個班級的老師的id和姓名
    # ret = Teacher.objects.annotate(cls_count=Count('classes')).filter(cls_count__gte=3).values('id','tname','cls_count')

    # 12.查詢學過編號1課程和編號2課程的同窗的學號,姓名
    # ret = Student.objects.filter(score__course__in=[1,2]).annotate(course_count=Count('score')).filter(course_count=2).values('id','sname','course_count')

    # 13.查詢只學過編號1課程和編號2課程的同窗的學號,姓名
    # ret1 = Student.objects.annotate(course_count=Count('score')).filter(course_count=2).values('id','sname','course_count')
    # ret2 = Student.objects.filter(score__course__in=[1,2]).annotate(course_count=Count('score')).filter(course_count=2).values('id','sname','course_count')
    # for i in ret1:
    #     for j in ret2:
    #         if i==j:
    #             print(i)
    # 這個方法好
    # ret = Student.objects.annotate(course_count=Count('score')).filter(score__course=1).filter(score__course=2).filter(course_count=2).values('id','sname')


    # 14.查詢所帶班級數最多的老師id和姓名
    # ret = Teacher.objects.annotate(cls_count=Count('classes')).values('id','tname','cls_count').order_by('-cls_count')[0]

    # 15.查詢有課程成績小於60分的同窗的學號,姓名
    # ret = Student.objects.filter(score__score__lt=60).annotate(stu_score=Count('score')).filter(stu_score__gte=1).values('id','sname','stu_score')

    # 16.查詢男生,女生的人數,按倒序排列
    # ret = Student.objects.values('gender').annotate(c=Count('id')).order_by('-c')

    # 17.查詢各個課程及相應的選修人數
    # ret = Course.objects.annotate(stu_count=Count('score')).values('cname','stu_count')

    # 18.查詢同時選修了物理課和生物課的學生id和姓名
    # ret = Student.objects.filter(score__course__cname__in=['物理','生物']).annotate(course_count=Count('score__course')).filter(course_count=2).values('id','sname','course_count')
    # ret = Student.objects.annotate(c=Count('score__course')).filter(c=2).filter(score__course__cname="物理").filter(score__course__cname="生物").values("id", "sname")

    # 19.檢索3課程分數小於60,按分數降序排列的同窗學號
    # ret = Course.objects.filter(score__course=3,score__score__lt=60).order_by('score__score').values('score__student','score__student__sname')

    # 20.檢查每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列
    # ret = Course.objects.annotate(score_avg=Avg('score__score')).order_by('score_avg','-id').values('id','cname','score_avg')

    # 21.查詢各科成績最高和最低分:以以下形式顯示:課程ID,最高分,最低分
    ret = Course.objects.annotate(max_score=Max('score__score'),min_score=Min('score__score')).values('id','cname','max_score','min_score')
    print(ret)
    return HttpResponse("ok")
相關文章
相關標籤/搜索