Django中的跨关系查询
作者:互联网
一、创建表
# 创建班级表 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)
标签:关系,name,models,Grade,查询,grade,grades,context,Django 来源: https://www.cnblogs.com/lxmtx/p/13494334.html