关系表介绍及使用  一对一关系1 2 xx = models.OneToOneField(to='表名' ,to_field='字段名' ,on_delete=models.CASCADE)   
  增加数据  方式11 2 3 4 5 6 7 8 9 10 11 12 new_author_detail = models.AuthorDetail.objects.create(         birthday='1979' ,         telephone='138' ,         addr='black'      )          models.Author.objects.create(         name='王涛' ,         age=18 ,                  authorDetail=new_author_detail      ) 
  方式21 2 3 4 5 6 7 8 9 10 11 new_author_detail = models.AuthorDetail.objects.create(         birthday='1979' ,         telephone='138' ,         addr='black'      )          models.Author.objects.create(         name='王涛' ,         age=18 ,         authorDetail_id=new_author_detail.id      ) 
  删除数据表一外键关联到表二,表一删除,不影响表2,表2删除会影响表1
1 2 models.AuthorDetail.objects.get(id =2 ).delete() models.Author.objects.get(id =3 ).delete() 
  更新数据1 2 3 4 5 6 models.Author.objects.filter (id =5 ).update(         name='崔老师' ,         age=16 ,         authorDetail=models.AuthorDetail.objects.get(id =5 ),         authorDetail_id=4 ,      ) 
  一对多关系1 xx = models.ForeignKey(to='表名' ,to_field='字段名' ,on_delete=models.CASCADE) 
  增加数据  方式1外键关联的直接赋值为某个对象即可
1 2 3 4 5 6 7 obj = models.Publish.objects.get(id =1 ) models.Book.objects.create(     title='李帅' ,     publishDate='2019-07-22' ,     price=3 ,     publishs=obj ) 
  方式21 2 3 4 5 6 7 8 obj = models.Publish.objects.get(id =1 ) models.Book.objects.create(         title='李帅test' ,         publishDate='2019-07-22' ,         price=3.5 ,         publishs_id=1                ) 
  删除数据1 2 models.Publish.objects.get(id =1 ).delete() models.Book.objects.get(nid=1 ).delete() 
  更新数据1 2 3 4 5 models.Book.objects.filter (pk=4 ).update( title='B哥的往事2' , publishs=models.Publish.objects.get(id =3 ), publishs_id=3 ,  	) 
  多对多关系1 xx = models.ManyToManyField(to='另外一个表名' )  
  添加数据1 2 book_obj = models.Book.objects.get(nid=1 )     book_obj.authors.add(*[1 ,2 ]) 
  删除数据1 2 3 4 5 6 7 book_obj = models.Book.objects.get(nid=6 ) book_obj.authors.remove(6 ) book_obj.authors.remove(*[5 ,6 ]) book_obj.authors.clear()  book_obj.authors.add(*[1 ,]) book_obj.authors.set ('1' ) book_obj.authors.set (['5' ,'6' ])  
  示例1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 from  django.db import  modelsfrom  django.db import  modelsclass  Author (models.Model ):      name=models.CharField( max_length=32 )     age=models.IntegerField()          authorDetail=models.OneToOneField(to='AuthorDetail' )                 def  __str__ (self ):         return  self.name class  AuthorDetail (models.Model ):    birthday=models.DateField()          telephone=models.CharField(max_length=32 )     addr=models.CharField( max_length=64 )     def  __str__ (self ):         return  self.addr class  Publish (models.Model ):    name=models.CharField( max_length=32 )     city=models.CharField( max_length=32 )     email=models.EmailField()       def  __str__ (self ):         return  self.name class  Book (models.Model ):    nid = models.AutoField(primary_key=True )     title = models.CharField( max_length=32 )     publishDate=models.DateField()     price=models.DecimalField(max_digits=5 ,decimal_places=2 )       publishs=models.ForeignKey(to="Publish" )     authors=models.ManyToManyField(to='Author' ,)     def  __str__ (self ):         return  self.title