orm exercises

Table diagram

 

 

models.py

from django.db import models

# Create your models here.


class Teacher(models.Model):
    tid=models.AutoField(primary_key=True)
    tname=models.CharField(max_length=32)
    classes=models.ManyToManyField("Klass")
    def __str__(self):
         return self.tname

class Grade(models.Model):
    gid=models.AutoField(primary_key=True)
    gname=models.CharField(max_length=32)

    def __str__(self):
         return self.gname

class Klass(models.Model):
    kid=models.AutoField(primary_key=True)
    kname=models.CharField(max_length=32)
    grade=models.ForeignKey("Grade",on_delete=models.CASCADE)
    def __str__(self):
         return self.kname


class Student(models.Model):
    sid=models.AutoField(primary_key=True)
    sname=models.CharField(max_length=32)
    gender=models.IntegerField(choices=((0,""),(1,"")))
    cls=models.ForeignKey("Klass",on_delete=models.CASCADE)
    def __str__(self):
         return self.sname


class Course(models.Model):
     cid=models.AutoField(primary_key=True)
     cname=models.CharField(max_length=32)
     teacher=models.ForeignKey("Teacher",on_delete=models.CASCADE)
     def __str__(self):
         return self.cname


class Score(models.Model):
    sid=models.AutoField(primary_key=True)
    student=models.ForeignKey("Student",on_delete=models.CASCADE)
    course=models.ForeignKey("Course",on_delete=models.CASCADE)
    score=models.IntegerField()

    def __str__(self):
        return str(self.student)+str(self.course)+str(self.score)

    # class Meta:
    #     unique_together = (("student","course"),)

Test subject:

1 , create your own test data;
 2 , the total number of student inquiry;
 3 , the query "biological" course and "physical" course grade student who has failed both id and name;
 4 , query the number of classes at each grade level, remove the maximum number of class the first three grades;
 5 , the query highest grade point average student id and name and grade point average;
 6 , query the number of students in each grade;
 7 , the query each student number, name, grade point average;
 8, student inquiry number "2 " in the student's name, the name of student achievement and curriculum highest score;
 9 , the query teacher surnamed "Li" in the number and brought the number of classes;
 10 , the query is less than the number of classes and grades id Grade 5 name;
 11 , query taught courses over two teachers of id and name;
 12, learned inquiry number "1" course and the number "2 " students of the course number, name;
 13 inquiries brought up to the number of classes teachers and id name;
 14 inquiries have less than 60 points of the course grade students number, name;
 15 , the number of inquiries boys and girls, according to the reverse order;
 16 inquiries with various courses and The number of elective;
 17 , while the query elective physics and biology classes the student id and name;
18, the search "3 " course fraction is less than 60, students learn number by score in descending order;
 19 ; query the average score for each course, the results of the average results in ascending order, the average score of the same, according to the course number in descending order
 20, query subjects highest score and lowest points: displayed in the following form: course ID, highest score, lowest score;

answer:

# # 1 Total number of queries students; 
    # RET1 = Student.objects.count () 
    # Print (RET1) 
    # # 2 student id and name queries biological or physical course curriculum results are qualified; 
    # RET2 = Score.objects.filter ( course__cname__in = [ "bio", "Physics"], score__gte = 60) .values ( "student__sname", "student__pk") 
    # RET2 = Score.objects.filter (course__cname__in = [ "bio", "Physics"], score__gte = 60) .values ( "student__pk") . annotate (c = Count ( "course")). filter (c = 2)

    # 3 queries the number of classes in each grade, the number of classes before taken up three grades name; 
    # RET = Grade.objects.annotate (C = the Count ( "Klass")) ORDER_BY ( "- C") [0:. 3] 
    # Print (RET)

    #
     # # 4 queries the highest grade point average student id and name and grade point average;

    # ret=Score.objects.values("student").annotate(avg_score=Avg("score")).order_by("-avg_score").values("student__sname","student__pk","avg_score")[0]
    # print(ret)


    # The number of queries # 5 students in each grade; 
    # . RET = Grade.objects.annotate (c = the Count ( "klass__student__pk")) values ( "gname", "c") 
    # Print (RET)


    # # 6 queries each student's school number, name, grade point average; 
    RET = Student.objects.values ( " sid " , " sname " ) .annotate (avg_score = Avg ( " score__score " ))
     Print (RET)
     # RET = Student.objects.annotate (avg_score = Avg ( " score__score")). values ( "sid", "sname", "avg_score")


    # ret=Student.objects.annotate(avg_score=Avg("score__score")).values("sname","pk","avg_score")
    # print(ret)

    # 7 Query student number "2" in the name of the student, the student achievement scores and the highest course name; 
    RET = Student.objects.filter (PK = 2) .order_by ( " -score__score " ) .values ( " sname " , " score__course__cname " , " score__score " ) [0]
     Print (RET)
    ret=Score.objects.filter(student__pk=2).order_by("-score").values("student__sname","course__cname","score")[0]
    print(ret)

    # ret=Score.objects.values("student").annotate(max_score=Max("score")).values("student__sname","max_score","course__cname")
    # ret = Score.objects.filter(student=2).order_by("-score").values("score", "course__cname", "student__sname")[0]
    # print(ret)

    # 8 queries each surname "Lee," the teacher brought the number of classes; 
    RET = Teacher.objects.filter (tname__istartswith = " small " ) .annotate (c = the Count ( " classes " .)) Values ( " tname " , " c " )
     Print (RET)

    # ret=Teacher.objects.filter(tname__startswith="李").annotate(c=Count("classes")).values("tname","c")

    # # 9 query id and the number of classes is less than Grade 5 Grade name; 
    Grade.objects.annotate (C = the Count ( " Klass " .)) Filter (c__lt = 5) .values ( " PK " , " gname " )

    # ret=Grade.objects.annotate(c=Count("klass")).filter(c__lt=5).values("pk","gname")

    # # 10 inquiries taught courses over two teachers of id and name; 

    RET = Teacher.objects.annotate (c = the Count ( " Course, " .)) Filter (c__gt = 2) .values ( " PK " , " tname " )


    # ret=Teacher.objects.annotate(c=Count("course")).filter(c__gt=2).values("pk","tname")


    # # 11 inquiry learned number "1" course and the number "2" of the course the students number, name; 

    RET = Student.objects.filter (score__course__cid = 1) .filter (score__course__cid = 2) .values ( " PK " , " sname " )
     Print (RET)

    # ret=Student.objects.filter(score__course__cid__in=[1,2]).values("pk","sname")
    #

    # # 12 查询所带班级数最多的老师id和姓名;
    ret=Teacher.objects.annotate(c=Count("classes")).order_by("-c").values("pk","tname")[0]
    # ret=Teacher.objects.annotate(c=Count("classes")).order_by("-c").values("pk","tname")[0]

    # # 13 inquiries have less than 60 points of course grade students number, name; 

    RET = Score.objects.filter (score__lt = 60) .values ( " student__sname " , " student__pk " ) .distinct ()
     Print (RET)
     # = Score.objects.filter RET (score__lt = 60) .values ( "student__sname", "student__pk"). DISTINCT () 
    #



    # # 14 inquiries boys, number of girls, according to the reverse order; 
    RET = Student.objects.values ( " Gender " ) .annotate (c = the Count (1)) order_by (. " -C " )
     # RET = Student.objects .values ( "gender"). annotate (c = Count (1)). order_by ( "c"). values ( "gender", "c")

    # # 15 inquiries each elective courses and the corresponding number; 
    RET = Score.objects.values ( " Course, " .) .Annotate (c = the Count (1)) values ( " course__cname " , " c " )
     Print (RET)
     # ret = Score.objects.values ( "course") . annotate (c = Count ( "student")). values ( "course__cname", "c")

    # # 16 queries simultaneously enrolled in physics and biology classes the student id and name; 
    # . RET = Student.objects.filter (score__course__cname__in = [ "physical", "bio"]) values ( "pk" , "sname")


    # # 17 retrieves "3" course fraction is less than 60, No. students learn by score in descending order; 

    Score.objects.filter (COURSE_ID = 3, score__lt = 60) .order_by ( " -score " ) .values ( " the student_id " )
     # RET = Score.objects.filter (course__cid = 3, score__lt = 60) .order_by ( "score"). values ( "student__sname", "student__pk") 
    #
     # # 18 inquiries each course grade point average, average results results in ascending order, the average score of the same, according to the course number in descending order; 

    RET = Score.objects.values ( " COURSE_ID " ) .annotate (C Avg. = ( " score " )) ORDER_BY (. " C " , "-course_id")
    #.. RET = Score.objects.values ( "Course,") Annotate (avg_score = Avg ( "Score")) order_by ( "avg_score", "Course,") 
    #
     # # 19 Discover all subjects of the highest and lowest points: to displayed as follows: course ID, highest score, lowest score; 
    RET = Course.objects.annotate (max_score = Max ( " score__score " ), min_score min = ( " score__score " .)) values ( " PK " , " max_score " , " min_score " )
     # RET = Course.objects.annotate (max_score = Max ( "score__score"), min_score Min = ( "score__score")). values ( "CNAME", "max_score", "min_score")
View Code

 

Guess you like

Origin www.cnblogs.com/one-tom/p/11872750.html