一、创建表
# 创建班级表 class Grade(models.Model): g_name = models.CharField(max_length=16) # 创建学生表 class Student(models.Model): s_name = models.CharField(max_length=16) s_grade = models.ForeignKey(Grade,on_delete=models.CASCADE)
二、查询
# 跨关系查询,查询姓名为mozili所在的班级 def get_grade(request): grades = Grade.objects.all().filter(student__s_name='mozili') context = { 'grades':grades, } return render(request, 'grade.html',context=context)