ForeignKey 一对多
创建模型Classes,Students 一个班级对应多个学生
from django.db import models # 一对多关系 # 班级表 class Classes(models.Model): cname = models.CharField(max_length=15) class Meta(): db_table = 'classes' # 学生表 class Students(models.Model): sname = models.CharField(max_length=15) # 在学生表中关联班级表,班级表的一条数据关联学生表中的多条数据 cid = models.ForeignKey(to='Classes', to_field='id', on_delete=models.CASCADE) # 第一个参数:是被关联的模型名称 # 第二个参数:是被关联的模型(表)名称的字段 # 第三个参数:当被关联的班级表classes中的一条数据被删除的时候,与之关联的学生表students中的所有数据也会被删除(必需) class Meta(): db_table = 'students'
根路由
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('app/', include('myapp.urls')), ]
子路由
from django.urls import path, re_path from . import views urlpatterns = [ path('otm_add_01/', views.onetomany_add_01), path('otm_add_02/', views.onetomany_add_02), path('otm_update/', views.onetomany_update), path('otm_search/', views.onetomany_search), path('otm_delete/', views.onetomany_delete), ]
视图
from django.http import HttpResponse from . import models # 一对多 # 增 def onetomany_add_01(request): # 在添加班级时添加学生 # 添加班级 class_01 = models.Classes() class_01.cname = 'class17' class_01.save() class_02 = models.Classes() class_02.cname = 'class18' class_02.save() # 添加学生并关联班级 student_01 = models.Students() student_01.sname = 'student1701' # 关联班级 student_01.cid = class_01 student_02 = models.Students() student_02.sname = 'student1702' student_02.cid = class_01 student_03 = models.Students() student_03.sname = 'student1801' student_03.cid = class_02 student_04 = models.Students() student_04.sname = 'student1802' student_04.cid = class_02 student_01.save() student_02.save() student_03.save() student_04.save() return HttpResponse('<script>alert("success");</script>') def onetomany_add_02(request): # 已有班级添加学生 class_03 = models.Classes.objects.all().get(id=3) student_05 = models.Students() student_05.sname = 'student1901' student_05.cid = class_03 student_05.save() return HttpResponse('<script>alert("success");</script>') # 查 def onetomany_search(request): # 通过学生查找班级 studentobj = models.Students.objects.all().get(id=1) print(studentobj.sname) # 学生对象.外键.班级信息 print(studentobj.cid.cname) # 通过班级查找学生 classobj = models.Classes.objects.all().get(cname='class19') print(classobj.cname) # 班级对象.学生类名(小写)_set.学生属性 print(classobj.students_set.all().values()) return HttpResponse('<script>alert("success");</script>') # 改 def onetomany_update(request): # 获取要转移到的班级并改名 tran_class = models.Classes.objects.all().get(cname='class19') tran_class.cname = 'class_19' tran_class.save() # 通过学生对象修改学生信息并转移班级 tran_student = models.Students.objects.all().get(id=1) tran_student.sname = 'student19_01' tran_student.cid = tran_class tran_student.save() # 通过班级对象修改学生信息 update_student = tran_class.students_set.all().last() update_student.sname = 'student1902' update_student.save() return HttpResponse('<script>alert("success");</script>') # 删 def onetomany_delete(request): # 如果删除班级,与之关联的学生也会被删除 delete_class = models.Classes.objects.all().get(id=4) delete_class.delete() # 如果删除学生,与之关联的班级不会有影响 delete_student = models.Students.objects.all().get(id=8) delete_student.delete() return HttpResponse('<script>alert("success");</script>')