一掩蛤、一對一關(guān)系(在任意一方添加)
1.創(chuàng)建新表:
class StudentInfo(models.Model):
tel = models.CharField(max_length=11, null=True, unique=True, verbose_name='手機(jī)號')
address = models.CharField(max_length=50, null=True, verbose_name='住址')
class Meta:
db_table = 'student_info'
2.在Student2中添加一對一關(guān)系
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='創(chuàng)建時(shí)間')
operate_time = models.DateTimeField(auto_now=True, verbose_name='操作時(shí)間')
math = models.DecimalField(max_digits=4, decimal_places=2, null=True)
chinese = models.DecimalField(max_digits=4, decimal_places=2, null=True)
# 一對一關(guān)系
stu_info = models.OneToOneField(StudentInfo, null=True, related_name='stu')
stu_info是StudentInfo的別稱鸵鸥,stu是Student2的別稱,null=True表示外鍵可以為空窥浪。
3.向studentinfo表中插入數(shù)據(jù)
def create_stu_info(request):
if request.method == 'GET':
data = {
'18200384770': '金牛區(qū)',
'18200384771': '金牛區(qū)',
'18200384772': '金牛區(qū)',
'18200384773': '金牛區(qū)',
'18200384774': '金牛區(qū)',
}
for k, v in data.items():
StudentInfo.objects.create(tel=k, address=v)
return HttpResponse('創(chuàng)建副表')
if request.method == 'POST':
pass
4.關(guān)聯(lián)表
def stu_add_stuinfo(request):
if request.method == 'GET':
# 給id為2的學(xué)生添加拓展表中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('綁定學(xué)生和拓展表的關(guān)系')
stu是學(xué)生對象物臂,stu.stu_info = StudentInfo.objects.get(id=1)表示把id=6的學(xué)生和id=1的信息關(guān)聯(lián)起來。
5.查詢
def sel_tel_by_stu(request):
if request.method == 'GET':
# 獲取id為2的學(xué)生的手機(jī)號
# 方法一
# 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('通過學(xué)生查找手機(jī)號')
反之
def sel_stu_by_tel(request):
if request.method == 'GET':
# 通過手機(jī)號查找學(xué)生
stu_info = StudentInfo.objects.get(tel='18200384770')
print(stu_info.stu.s_name)
return HttpResponse('通過手機(jī)號查找學(xué)生')
二卒稳、一對多關(guān)系
1.創(chuàng)建表
class Grade(models.Model):
g_name = models.CharField(max_length=10, unique=True, verbose_name='班級名稱')
class Meta:
db_table = 'grade'
2.添加一對多關(guān)系(在多的一方添加)
# 多對一關(guān)系
g = models.ForeignKey(Grade, null=True, related_name='stu')
3.插入數(shù)據(jù)
def create_grade(request):
if request.method == 'GET':
g = Grade()
g.g_name = 'RTX2080'
g.save()
return HttpResponse('創(chuàng)建班級')
4.添加關(guān)系(和一對一相同)
5.查詢
def sel_stu_by_grade(request):
if request.method == 'GET':
# 查詢python1805的學(xué)生蹋半,獲取姓名
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('根據(jù)班級查找學(xué)生')
三、多對多關(guān)系
1.創(chuàng)建表
class Course(models.Model):
c_name = models.CharField(max_length=10, null=True)
class Meta:
db_table = 'course'
2.添加多對多關(guān)系(在任意一方添加)
# 多對多關(guān)系
c = models.ManyToManyField(Course, null=True)
3.插入數(shù)據(jù)
def create_course(request):
if request.method == 'GET':
c = Course()
c.c_name = 'python'
c.save()
return HttpResponse('創(chuàng)建課程')
4.添加關(guān)系
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的學(xué)生的關(guān)聯(lián)關(guān)系
c = Course.objects.get(c_name='java')
c.student2_set.add(4)
return HttpResponse('創(chuàng)建學(xué)生課程關(guān)聯(lián)')
5.查詢
def del_stu_course(request):
if request.method == 'GET':
# 刪除關(guān)聯(lián)
c = Course.objects.get(c_name='java')
c.student2_set.remove(4)
return HttpResponse('刪除學(xué)生課程關(guān)聯(lián)')
總結(jié):
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.中間表加數(shù)據(jù)
c.d.add(d_ id)
c. d. remove(d id)