經過在「多」方模型類中添加外鍵屬性 ForeignKey django
class School(models.Model): #「一方」模型 name = models.CharField(max_length=30) address = models.CharField(max_length=30) history = models.TextField(null=True) class Student(models.Model): name = models.CharField(max_length=20) age = models.InterField() score = models.FloatField() sex = models.CharField(max_length=10) school = models.ForeignKey(School,on_delete=models.CASCADE)
方式一:經過"多"方模型的外鍵類屬性關聯"一"方模型的實例化對象ui
#添加學校對象(「一」方模型) school1 = School.objects.create(name='清華大學',address=‘北京海淀區’) school2 = School.objects.create(name='北京大學',address='北京',history='北京大學是一所知名大學') school3 = School.objects.create(name='西安交通大學',address='西安',history='西安交大是一所很好的大學') #經過外鍵類屬性關聯 stu1 = Student.objects.create(name='張三',age=20,score=93,sex='男',school = School1)
方式二:經過「多」方對應表的外鍵關聯"一"方 spa
#兩個學生都上id爲3的學校 stu3 = Student.objects.careate(name='李志',age=40,score=89,sex='男',school_id=3) stu4 = Student.objects.careate(name='阿瑪尼',age=35,score=89,sex='女',school_id=3)
一方的實例化對象.多方模型類名小寫_set.all() code
#查詢1號學校全部的學生: school = School.objects.get(id=1) students = school.student_set.all()
經過多方設置的關聯類屬性查詢對象
#查詢5號學生對應的學校: student = Student.objects.get(id=5) school = student.school
在負責維護關係的「一」方添加OneToOneField型的類屬性blog
class Person(models.Model): name = modles.CharField(max_length=20) age = models.IntergerField() sex = models.CharField(max_length=10) class Card(models.Model): cardno = models.CharField(max_length=20,unique=True) #卡號類屬性 color = models.CharField(max_length=10) person = models.OneToOneField(Person,on_delete=models.CASCADE)#使用OneToOneField進行‘一對一’關聯
經過主動一方模型類屬性,關聯另外一方對象get
#建立人對象 per = Person(name='張三',age=20,sex='男') per.save() #經過Card類的person屬性關聯 card = Card(cardno='zs123456',color='綠卡',person=per) card.save()
經過對應表到的惟一外鍵字段關聯 it
per1 = Person(name='李四',age=22,sex='男') per1.save() card1 = Card.objects.create(cardno='ls123456',color='黃色',person_id=per1.id)
使用模型類中維護關係的那個類屬性 io
#查詢3號卡關聯的人class
card = Card.objects.get(id=3)
per = card.person # 使用關聯的類屬性查詢
使用對方模型類名的小寫
#查詢1號人的卡: per1 = Person.objects.get(id=1) card1 = per1.card # 使用對方模型類名的小寫
from django.db import models class Member(models.Model): # 成員模型 name = models.CharField(max_length=20) age = models.IntegerField() sex = models.CharField(max_length=10) def __str__(self): return self.name class Community(models.Model): # 社團模型 name = models.CharField(max_length=20) buildtime = models.DateField() members = models.ManyToManyField(Member,through="Relation") def __str__(self): return self.name class Relation(models.Model): member = models.ForeignKey(Member,on_delete=models.CASCADE) community = models.ForeignKey(Community,on_delete=models.CASCADE) join_reason = models.CharField(max_length=100)
經過第三方模型對象維護
建立Member對象: member1 = Member.objects.create(name='馬小跳',age=20,sex='男') member2 = Member.objects.create(name='李麗麗',age=25,sex='女') member3 = Member.objects.create(name='黃大牛',age=35,sex='男') 建立Community對象: community1 = Community.objects.create(name='天涯吉他社',buildtime=date(2016,6,6)) community2 = Community.objects.create(name='讀書會',buildtime=date(2017,10,1)) community3 = Community.objects.create(name='瑜伽協會',buildtime=date(2008,9,3)) 建立Relation關係對象,經過實例化對象關聯: r1 = Relation.objects.create(member=member2,community=community1,join_reason='好玩') 經過外鍵值關聯: r2 = Relation.objects.create(member_id=member3.id,community_id=community1.id,join_reason='交朋友')
經過關聯的類屬性查詢另外一方.all()
查詢3號社團全部的成員: 方式一:藉助中間模型查詢: community = Community.objects.get(id=3) relations = Relation.objects.filter(community=community) for r in relations: print(r.member) 方式二:藉助關聯的類屬性直接查詢 all_members = community.members.all() for m in all_members: print(m)
對方模型類名小寫_set.all()
查詢1號成員參加的社團: member1 = Member.objects.get(id=1) all_community = member1.community_set.all() for c in all_community: print(c)