三种关系 一对一,一对多,多对多
1 一对一 2 xx = models.OneToOneField(to='表名',to_field='字段名',on_delete=models.CASCADE) #删除时的一些级联效果,to_field可以不写,默认是关联到另一张表的主键,on_delete在1.x版本的django中不用写,默认是级联删除的,2.x版本的django要写. 3 4 一对多 5 xx = models.ForeignKey(to='表名',to_field='字段名',on_delete=models.CASCADE) 6 多对多 7 xx = models.ManyToManyField(to='另外一个表名') #这是自动创建第三表 8 9 示例 10 from django.db import models 11 12 # Create your models here. 13 14 from django.db import models 15 16 # Create your models here. 17 #作者表 18 class Author(models.Model): #比较常用的信息放到这个表里面 19 name=models.CharField( max_length=32) 20 age=models.IntegerField() 21 # authorDetail=models.OneToOneField(to="AuthorDetail",to_field="nid",on_delete=models.CASCADE) 22 authorDetail=models.OneToOneField(to='AuthorDetail') #一对一到AuthorDetail表 生成为表字段之后,会自动变为authorDetail_id这样有一个名称 23 24 # 外键字段 -- 外键字段名_id 25 26 # foreign+unique 27 28 def __str__(self): 29 return self.name 30 31 #作者详细信息表 32 class AuthorDetail(models.Model): 33 birthday=models.DateField() 34 # telephone=models.BigIntegerField() 35 telephone=models.CharField(max_length=32) 36 addr=models.CharField( max_length=64) 37 def __str__(self): 38 return self.addr 39 40 41 #出版社表 和 书籍表 是 一对多的关系 42 class Publish(models.Model): 43 name=models.CharField( max_length=32) 44 city=models.CharField( max_length=32) 45 email=models.EmailField() #charfield -- asdfasdf 46 def __str__(self): 47 return self.name 48 49 #书籍表 50 class Book(models.Model): 51 nid = models.AutoField(primary_key=True) 52 title = models.CharField( max_length=32) 53 publishDate=models.DateField() 54 price=models.DecimalField(max_digits=5,decimal_places=2) #decimal(16,2) 55 publishs=models.ForeignKey(to="Publish") 56 authors=models.ManyToManyField(to='Author',) 57 58 def __str__(self): 59 return self.title 60 61 #手动创建第三张表,暂时忽略 62 # class BookToAuthor(models.Model): 63 # book_id = models.ForeignKey(to='Book') 64 # author_id = models.ForeignKey(to='Author') 65 # # xx = models.CharField(max_length=12)
增删改查
增
1 #1 增 2 #1.1 一对一增加 3 # new_author_detail = models.AuthorDetail.objects.create( 4 # birthday='1979-08-08', 5 # telephone='138383838', 6 # addr='黑龙江哈尔滨' 7 # ) 8 # obj = models.AuthorDetail.objects.filter(addr='山西临汾').first() 9 10 #方式1 11 # models.Author.objects.create( 12 # name='王涛', 13 # age='40', 14 # authorDetail=new_author_detail, 15 # ) 16 # 方式2 常用 17 # models.Author.objects.create( 18 # name='王涛', 19 # age='40', 20 # authorDetail_id=obj.id, 21 # ) 22 23 # 一对多 24 #方式1 25 # obj = models.Publish.objects.get(id=2) 26 # models.Book.objects.create( 27 # title = '李帅的床头故事', 28 # publishDate='2019-07-22', 29 # price=3, 30 # # publishs=models.Publish.objects.get(id=1), 31 # publishs=obj, 32 # 33 # ) 34 # 方式2 常用 35 # models.Book.objects.create( 36 # title='李帅的床头故事2', 37 # publishDate='2019-07-21', 38 # price=3.5, 39 # # publishs=models.Publish.objects.get(id=1), 40 # publishs_id=obj.id 41 # 42 # ) 43 44 # 多对多 45 # 方式1 常用 46 # book_obj = models.Book.objects.get(nid=1) 47 # book_obj.authors.add(*[1,2]) 48 # 方式2 49 # author1 = models.Author.objects.get(id=1) 50 # author2 = models.Author.objects.get(id=3) 51 # book_obj = models.Book.objects.get(nid=5) 52 # book_obj.authors.add(*[author1,author2])
删
1 一对一和一对多的删除和单表删除是一样的 2 # 一对一 表一外键关联到表二,表一删除,不影响表2,表2删除会影响表1 3 # models.AuthorDetail.objects.get(id=2).delete() 4 # models.Author.objects.get(id=3).delete() 5 6 # 一对多 7 # models.Publish.objects.get(id=1).delete() 8 # models.Book.objects.get(nid=1).delete() 9 10 # 多对多关系删除 11 # book_obj = models.Book.objects.get(nid=6) 12 # book_obj.authors.remove(6) 13 # book_obj.authors.remove(*[5,6]) 14 # book_obj.authors.clear() 15 # book_obj.authors.add(*[1,]) 16 # book_obj.authors.set('1') 17 # book_obj.authors.set(['5','6']) #删除然后更新
更新
1 # 更新 2 # 一对一 3 # models.Author.objects.filter(id=5).update( 4 # name='崔老师', 5 # age=16, 6 # # authorDetail=models.AuthorDetail.objects.get(id=5), 7 # authorDetail_id=4, 8 # ) 9 #一对多 10 # models.Book.objects.filter(pk=4).update( 11 # title='B哥的往事2', 12 # # publishs=models.Publish.objects.get(id=3), 13 # publishs_id=3, 14 # ) 15 16 #一对多 17 models.Publish.objects.filter(pk=2).update( 18 id=4, # 没有级联更新,报错!! 19 )