一、一对一关系(在任意一方添加)
1.创建新表:
class StudentInfo(models.Model):
tel = models.CharField(max_length=11, null=True, unique=True, verbose_name='手机号')
address = models.CharField(max_length=50, null=True, verbose_name='住址')
class Meta:
db_table = 'student_info'
2.在Student2中添加一对一关系
class Student2(models.Model):
s_name = models.CharField(max_length=10, unique=True, verbose_name='姓名')
s_age = models.IntegerField(default=19, verbose_name='年龄')
s_sex = models.BooleanField(default=1, verbose_name='性别')
create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
operate_time = models.DateTimeField(auto_now=True, verbose_name='操作时间')
math = models.DecimalField(max_digits=4, decimal_places=2, null=True)
chinese = models.DecimalField(max_digits=4, decimal_places=2, null=True)
# 一对一关系
stu_info = models.OneToOneField(StudentInfo, null=True, related_name='stu')
stu_info是StudentInfo的别称,stu是Student2的别称,null=True表示外键可以为空。
3.向studentinfo表中插入数据
def create_stu_info(request):
if request.method == 'GET':
data = {
'18200384770': '金牛区',
'18200384771': '金牛区',
'18200384772': '金牛区',
'18200384773': '金牛区',
'18200384774': '金牛区',
}
for k, v in data.items():
StudentInfo.objects.create(tel=k, address=v)
return HttpResponse('创建副表')
if request.method == 'POST':
pass
4.关联表
def stu_add_stuinfo(request):
if request.method == 'GET':
# 给id为2的学生添加拓展表中id=2的信息,
# stu = Student2.objects.get(id=2)
# stu.stu_info_id = 2
# stu.save()
# 方法二
stu = Student2.objects.get(id=6)
stu.stu_info = StudentInfo.objects.get(id=1)
stu.save()
return HttpResponse('绑定学生和拓展表的关系')
stu是学生对象,stu.stu_info = StudentInfo.objects.get(id=1)表示把id=6的学生和id=1的信息关联起来。
5.查询
def sel_tel_by_stu(request):
if request.method == 'GET':
# 获取id为2的学生的手机号
# 方法一
# stu = Student2.objects.filter(id=2).first()
# info_id = stu.stu_info_id
# stu_info = StudentInfo.objects.get(pk=info_id)
# 方法二
# stu = Student2.objects.get(id=2)
# stu_info = stu.stu_info
# tel = stu_info.tel
# print(tel)
# 方法三
stu = Student2.objects.get(id=2)
print(stu.stu_info.tel)
return HttpResponse('通过学生查找手机号')
反之
def sel_stu_by_tel(request):
if request.method == 'GET':
# 通过手机号查找学生
stu_info = StudentInfo.objects.get(tel='18200384770')
print(stu_info.stu.s_name)
return HttpResponse('通过手机号查找学生')
二、一对多关系
1.创建表
class Grade(models.Model):
g_name = models.CharField(max_length=10, unique=True, verbose_name='班级名称')
class Meta:
db_table = 'grade'
2.添加一对多关系(在多的一方添加)
# 多对一关系
g = models.ForeignKey(Grade, null=True, related_name='stu')
3.插入数据
def create_grade(request):
if request.method == 'GET':
g = Grade()
g.g_name = 'RTX2080'
g.save()
return HttpResponse('创建班级')
4.添加关系(和一对一相同)
5.查询
def sel_stu_by_grade(request):
if request.method == 'GET':
# 查询python1805的学生,获取姓名
g = Grade.objects.get(g_name='python1805')
stus_names = g.stu.values('s_name')
print(stus_names)
stu = Student2.objects.filter(s_name='jerry').first()
grade = stu.g.g_name
print(grade)
return HttpResponse('根据班级查找学生')
三、多对多关系
1.创建表
class Course(models.Model):
c_name = models.CharField(max_length=10, null=True)
class Meta:
db_table = 'course'
2.添加多对多关系(在任意一方添加)
# 多对多关系
c = models.ManyToManyField(Course, null=True)
3.插入数据
def create_course(request):
if request.method == 'GET':
c = Course()
c.c_name = 'python'
c.save()
return HttpResponse('创建课程')
4.添加关系
def create_stu_course(request):
if request.method == 'GET':
# 让jerry选择课程(python)
# stu = Student2.objects.get(s_name='jerry')
# 添加add方法
# stu.c.add(1)
# 添加java和id=4的学生的关联关系
c = Course.objects.get(c_name='java')
c.student2_set.add(4)
return HttpResponse('创建学生课程关联')
5.查询
def del_stu_course(request):
if request.method == 'GET':
# 删除关联
c = Course.objects.get(c_name='java')
c.student2_set.remove(4)
return HttpResponse('删除学生课程关联')
总结:
class C:
aOneToOneFieLd(A)
b = ForeignKey(B)
d = Many ToManyFiLed(D)
1.有c对象,分别去查A, B, D的信息
C=C()
查询a对象信息: c.a
查询b对象信息: c.b.all()
查询d对象信息: c.d.filter().aLL()
2.反向查询。已知a, b, d对象查询c对象信息
通过a查询c对象信息: a.c
通过b查询c对象信息: b.c_ set.filter().alL()
通过d查询c对象信息: d.c_ set.all()
3.中间表加数据
c.d.add(d_ id)
c. d. remove(d id)