标签:length his integer object uniq key div .com objects
通过在“多”方模型类中添加外键属性 ForeignKey
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)
方式一:通过"多"方模型的外键类属性关联"一"方模型的实例化对象
#添加学校对象(“一”方模型) 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)
方式二:通过“多”方对应表的外键关联"一"方
#两个学生都上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()
#查询1号学校所有的学生: school = School.objects.get(id=1) students = school.student_set.all()
通过多方设置的关联类属性查询
#查询5号学生对应的学校: student = Student.objects.get(id=5) school = student.school
在负责维护关系的“一”方添加OneToOneField型的类属性
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进行‘一对一’关联
通过主动一方模型类属性,关联另一方对象
#创建人对象 per = Person(name=‘张三‘,age=20,sex=‘男‘) per.save() #通过Card类的person属性关联 card = Card(cardno=‘zs123456‘,color=‘绿卡‘,person=per) card.save()
通过对应表到的唯一外键字段关联
per1 = Person(name=‘李四‘,age=22,sex=‘男‘) per1.save() card1 = Card.objects.create(cardno=‘ls123456‘,color=‘黄色‘,person_id=per1.id)
使用模型类中维护关系的那个类属性
#查询3号卡关联的人
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)
标签:length his integer object uniq key div .com objects
原文地址:https://www.cnblogs.com/huiyichanmian/p/10268824.html