查询练习
2.models.py 创建表关系
from django.db import models # Create your models here. # 1.老师表 class Teacher(models.Model): tid = models.AutoField(primary_key=True) tname = models.CharField(max_length=32) class Meta: db_table="teacher" def __str__(self): return self.tname # 2.课程表 class Course(models.Model): cid = models.AutoField(primary_key=True) cname = models.CharField(max_length=32) #老师和课程 一对多 teaher =models.ForeignKey(to="Teacher",on_delete=models.CASCADE) class Meta: db_table="course" #3. 年纪表 class Class_grade(models.Model): gid = models.AutoField(primary_key=True) gname = models.CharField(max_length=32) class Meta: db_table="grade" #4.班级表 class Class(models.Model): cid = models.AutoField(primary_key=True) caption = models.CharField(max_length=32) #年级和班级是一对多关系 calss_grade = models.ForeignKey(to="Class_grade", on_delete=models.CASCADE) #班级和老师多对多 teahcer=models.ManyToManyField(to="Teacher") class Meta: db_table="class" #5.学生表 class Student(models.Model): sid = models.AutoField(primary_key=True) sname = models.CharField(max_length=32) gender = models.CharField(max_length=32) #学生表和班级表 多对一 cls= models.ForeignKey(to="Class",on_delete=models.CASCADE) class Meta: db_table="student" #6.成绩表 class Score(models.Model): sid = models.AutoField(primary_key=True) score =models.IntegerField() # #成绩和课程一对多 # course =models.ForeignKey(to="Course", on_delete=models.CASCADE) # #成绩和学生一对多 student = models.ForeignKey(to="Student", on_delete=models.CASCADE) class Meta: db_table="score" #