Django's practice orm

models (creating tables):

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()

Insert data:

DEF orm_insert (Request):
     "" " ORM table record inserted " "" 

    # teacher inserted table 
    T1 = Teacher.objects.create (tname = " John Doe " ) 
    T2 = Teacher.objects.create (tname = " John Doe " ) 
    T3 = Teacher.objects.create (tname = " Wang Wu " ) 

    # grades table insertion 
    G1 = Grade.objects.create (gname = " grade " ) 
    G2 = Grade.objects.create (gname = " second grade " ) 
    G3 = Grade.objects.the Create (gname = " third grade" ) 

    # Curriculum insert 
    c1 = Course.objects.create (CNAME = " bio " , teacher_id = 1 ) 
    c2 = Course.objects.create (CNAME = " sports " , teacher_id = 2 ) 
    c3 = Course.objects.create ( CNAME = " physical " , teacher_id =. 3 ) 


    # class table 
    CLl = Classes.objects.create (Caption = " year class " , grade_id =. 1 ) 
    CL11 = Classes.objects.create (Caption = " year class two " , 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)


    #Student Table 
    S1 = Student.objects.create (sname = " Liu Longkang " , Gender = ' M ' , = classes_id. 1 ) 
    S2 = Student.objects.create (sname = " chubby " , Gender = ' M ' , =. 1 classes_id ) 
    S3 = Student.objects.create (sname = " Bob " , Gender = ' M ' , classes_id = 2 ) 
    S4 = Student.objects.create (sname = " red " , Gender = ' F ' ,classes_id=3classes_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")

Query Exercise:

DEF orm_search (Request):
     "" " ORM job inquiries " "" 
    # 1 query total number of students; 
    # RET = Student.objects.aggregate (STUDENT_COUNT = the Count ( 'the above mentioned id')) 
    # RET = Student.objects.count ( ) 

    # 2. query biology curriculum and course grade physics student who has failed both id and name 
    # RET = Student.objects.annotate (JGS = the Count ( 'id')). filter (score__course__cname__in = [ 'biological', 'physical'] , score__score__gte = 60, 2 = JGS) .values ( "ID", "sname") 

    # 3. query the number of classes for each grade, the number of classes taken up before the two grades 
    # RET = Grade.objects.annotate (cls_count . = the Count ( 'classes__grade')) order_by (. '- cls_count') values ( 'gname', 'cls_count') 

    # 4. query highest grade point average of the students' grade point average as well as the names and id 
    #. Student.objects.annotate RET = (Avg. avg_score = ( 'score__score')) ORDER_BY. ( '- avg_score') values ( 'ID', 'sname', 'avg_score') [0] 

    # 5. The query each year the number of students 
    # RET = Grade.objects.annotate (stu_count the Count = ( 'classes__student__id')). values ( 'gname', 'stu_count') 

    # 6. queries each student's school number, name, grade point average 
    # RET = Student.objects.annotate (stu_avg = Avg ( 'score__score')). values ( 'the above mentioned id', 'sname', 'stu_avg') 

    # 7. student name query student number 2, which is the highest student achievement and curriculum name score 
    # 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 teacher surnamed the query number and brought the class number 
    # RET = Teacher.objects.filter (tname__startswith = 'Li') .annotate (cls_count = Count ( 'classes')). Values ( 'tname', 'cls_count') 

    # Grade and Grade name 9. query id number of the classes is less than 3 
    # RET = Grade.objects.annotate (cls_count the Count = ( 'classes')). values ( 'id', 'gname', 'cls_count' ) .filter (cls_count__lt = 3) 

    # 10. query taught courses over two of the teacher's name and id 
    # RET = Teacher.objects.annotate (cour_count the Count = ( 'course,')). filter (cour_count__gte = 1). values ( 'id', 'tname', 'cour_count') 

    # 11. query teacher taught over three classes of id and name 
    # RET = Teacher.objects.annotate (cls_count the Count = ( 'classes')). filter (cls_count__gte = 3) .values ( ' id', 'tname', 'cls_count ') 

    # 12. Query learned number 1 and number 2 Course curriculum students learn, name 
    #ret = Student.objects.filter (score__course__in = [1,2 ]). annotate (course_count = Count ( 'score')). filter (course_count = 2) .values ( 'id', 'sname', 'course_count') 

    # 13 inquiries only had number 1 and number 2 course curriculum students number, name 
    # RET1 = Student.objects.annotate (course_count the Count = ( 'Score')). filter (course_count = 2) .values ( ' ID ',' sname ',' course_count ') 
    # RET2 = Student.objects.filter (= score__course__in [1,2]). Annotate (course_count the Count = (' Score ')). filter (course_count = 2) .values ( 'the above mentioned id', 'sname', 'course_count') 
    # for i in RET1: 
    #      for J in RET2: 
    #          IF i == J: 
    #              Print (i) 
    # this method is good 
    #= Student.objects.annotate RET (course_count the Count = ( 'Score')). filter (score__course =. 1) .filter (score__course = 2) .filter (course_count = 2) .values ( 'ID', 'sname') 


    # 14. the inquiry brought the largest number of classes and teachers id name 
    # RET = Teacher.objects.annotate (cls_count the count = ( 'classes')). values (' id ',' tname ',' cls_count '). order_by (' -cls_count ') [0] 

    # 15. the query has less than 60 points of course grade students number, name 
    # RET = Student.objects.filter (score__score__lt = 60) .annotate (stu_score the Count = (' score ')). filter (stu_score__gte =. 1) .values ( 'ID', 'sname', 'stu_score') 

    # number 16. query boys and girls, according to the reverse order 
    # RET = Student.objects.values ( 'Gender'). Annotate ( the Count = C ( 'ID')) ORDER_BY (. '- C') 

    # . 17.Query each course and the corresponding number of elective 
    #= Course.objects.annotate RET (= stu_count the Count ( 'Score')). values ( 'CNAME', 'stu_count') 

    # 18. The query while enrolled in physics and biology classes the student's name and id 
    # RET = Student. objects.filter (score__course__cname__in = [ 'physical', 'biological']). annotate (course_count = Count ( 'score__course')). filter (course_count = 2) .values ( 'id', 'sname', 'course_count') 
    # RET = Student.objects.annotate (C = the Count ( 'score__course')). filter (C = 2) .filter (score__course__cname = "physical") .filter (score__course__cname = "bio") .values ( "id", "sname") 

    # 19. a program retrieves 3 fraction is less than 60, students learn by score in descending order of number 
    # RET = Course.objects.filter (score__course = 3, score__score__lt = 60) .order_by ( 'score__score'). values ( ' score__student ','score__student__sname ') 

    # 20. A check of the average score of each course, the results of the average results in ascending order, the same as the average score, in descending order according to the course number 
    #= Course.objects.annotate RET (score_avg Avg. = ( 'score__score')) ORDER_BY.. ( 'score_avg', '- ID') values ( 'ID', 'CNAME', 'score_avg') 

    # 21. A query subjects results maximum and minimum points: in the form shown in the following: ID course, the highest score, the lowest score 
    RET = Course.objects.annotate (max_score = Max ( ' score__score ' ), min = min_score ( ' score__score ' .)) values ( ' ID ' , ' CNAME ' , ' max_score ' , ' min_score ' )
     Print (RET)
     return the HttpResponse ( " OK ")

Guess you like

Origin www.cnblogs.com/leixiaobai/p/11068118.html