pycharm Develop Django Project ORM Job Reference Answer:

ORM Job Reference Answer:

  1. Query the ID and average scores of students with average score greater than 60 points;

     rows = Student.objects.annotate(avg=Avg("score__number")).filter(avg__gte=60).values("id","avg")
     for row in rows:
         print(row)
    
  2. Query all students’ ID, name, number of courses, total scores;

     rows = Student.objects.annotate(course_nums=Count("score__course"),total_score=Sum("score__number"))
     .values("id","name","course_nums","total_score")
     for row in rows:
         print(row)
    
  3. Query the number of teachers “Li”;

     teacher_nums = Teacher.objects.filter(name__startswith="Li") .count () Print (Teacher_nums)
  4. Query the ID, name of the classmates who have not learned the “Huang Teacher” class;

     rows = Student.objects.exclude(score__course__teacher__name="Huang Teacher") .VALUES'id','name')
     for row in rows:
         print(row)
    
  5. Query the ID, name of all the classmates who have learned the courses 1 and 2;

     rows = Student.objects.filter(score__course__in=[1,2]).distinct().values('id','name')
     for row in rows:
         print(row)
    
  6. Queze the students’ sickness, name of all the classes of “Huang Teacher” teaching;

     rows = Student.objects.annotate(nums=Count("score__course",filter=Q(score__course__teacher__name='Huang teacher'))))) .filter (nums = course.Objects.Filter (teacher__name ='Huang teacher') .count ()). VALUES'id','name')
     for row in rows:
         print(row)
    
  7. Query all courses less than 60 points of classmates ID and name;

     students = Student.objects.exclude(score__number__gt=60)
     for student in students:
         print(student)
    
  8. Query the ID, name of the classmates who do not learn all lessons;

     students = Student.objects.annotate(num=Count(F("score__course"))).filter(num__lt=Course.objects.count()).values('id','name')
     for student in students:
         print(student)
    
  9. Query all students’ names, average, and sort from high to low in accordance with the average division;

     students = Student.objects.annotate(avg=Avg("score__number")).order_by("-avg").values('name','avg')
     for student in students:
         print(student)
    
  10. Query the highest and minimum scores of each subject, show: course ID, course name, highest, minimum:

    courses = Course.objects.annotate(min=Min("score__number"),max=Max("score__number")).values("id",'name','min','max')
    for course in courses:
        print(course)
    
  11. Query the average score of each course and sort according to the average score;

    courses = Course.objects.annotate(avg=Avg("score__number")).order_by('avg').values('id','name','avg')
    for course in courses:
        print(course)
    
  12. How many girls have a total of statistics, how many boys are

    rows = Student.objects.aggregate(male_num=Count("gender",filter=Q(gender=1)),female_num=Count("gender",filter=Q(gender=2)))
    print(rows)
    
  13. Plus each course of “Huang Teacher” with 5 points above the original basis;

    rows = Score.objects.filter(course__teacher__name='Huang Teacher') .update (Number = f)"number")+5)
    print(rows)
    
  14. Query the ID, name, and non-time courses of two or more classmates;

    students = Student.objects.annotate(bad_count=Count("score__number",filter=Q(score__number__lt=60))).filter(bad_count__gte=2).values('id','name','bad_count')
    for student in students:
        print(student)
    
  15. Query the number of candidates for each class;

    courses = Course.objects.annotate(student_nums=Count("score__student")).values('id','name','student_nums')
    for course in courses:
        print(course)