1 class Author(models.Model): #比较常用的信息放到这个表里面 2 name=models.CharField( max_length=32) 3 age=models.IntegerField() 4 authorDetail=models.OneToOneField(to="AuthorDetail",to_field="nid",on_delete=models.CASCADE) 5 authorDetail=models.OneToOneField(to='AuthorDetail') #一对一到AuthorDetail表 生成为表字段之后,会自动变为authorDetail_id这样有一个名称 6 7 # 外键字段 -- 外键字段名_id 8 9 # foreign+unique 10 11 def __str__(self): 12 return self.name 13 14 15 #作者详细信息表 16 class AuthorDetail(models.Model): 17 birthday=models.DateField() 18 # telephone=models.BigIntegerField() 19 telephone=models.CharField(max_length=32) 20 addr=models.CharField( max_length=64) 21 def __str__(self): 22 return self.addr 23 24 25 #出版社表 和 书籍表 是 一对多的关系 26 class Publish(models.Model): 27 name=models.CharField( max_length=32) 28 city=models.CharField( max_length=32) 29 email=models.EmailField() #charfield -- asdfasdf 30 def __str__(self): 31 return self.name 32 33 #书籍表 34 class Book(models.Model): 35 nid = models.AutoField(primary_key=True) 36 title = models.CharField( max_length=32) 37 publishDate=models.DateField() 38 price=models.DecimalField(max_digits=5,decimal_places=2) #decimal(16,2) 39 publishs=models.ForeignKey(to="Publish") 40 authors=models.ManyToManyField(to='Author',) 41 42 def __str__(self): 43 return self.title
增删改查
增加
1 一对一 2 models.Author.objects.create(name='xx',age=18,authorDetail=mdoels.AuthorDetail.Objects.get(id=1)) 3 models.Author.objects.create(name='xx',age=18,authorDetail_id=2) 4 5 一对多 6 models.Book.objects.create(xx=xx,publishs=mdoels.Publish.Objects.get(id=1)) 7 models.Book.objects.create(xx=xx,publishs_id=2) 8 9 多对多 10 book_obj = models.Book.objects.get(id=1) 11 book_obj.authors.add(*[author_obj1,author_obj2,..]) 12 book_obj.authors.add(*[1,2,3...])
删除
1 一对一 2 models.Author.objects.filter(id=1).delete() 3 一对多 4 models.Book.objects.filter(id=1).delete() 5 多对多 6 book_obj = models.Book.objects.get(id=1) 7 book_obj.authors.remove(1,2,3,4) 8 book_obj.authors.remove(*[1,2,...]) 9 10 book_obj.authors.clear() 11 12 book_obj.authors.set(['1','2',...]) :clear -- add
改
1 一对一 2 models.Author.objects.filter(id=1).update( 3 authorDetail=mdoels.AuthorDetail.Objects.get(id=1) 4 ) 5 models.Author.objects.filter(id=1).update( 6 authorDetail_id=2, 7 ) 8 一对多 9 models.Book.objects.filter(id=1).update( 10 publishs=mdoels.Publish.Objects.get(id=1) 11 ) 12 models.Book.objects.filter(id=1).update( 13 publishs_id=2, 14 ) 15 16 多对多 17 book_obj.authors.set(['1','2',...]) :clear -- add
今日内容
查询
正向查询和反向查询
关系属性(字段)写在哪个类(表)里面,从当前类(表)的数据去查询它关联类(表)的数据叫做正向查询,反之叫做反向查询
1 #查询 2 # 一对一 3 # 正向查询 4 #1 查询崔老师的电话号 5 # author_obj = models.Author.objects.filter(name='崔老师').first() 6 # # print(author_obj.authorDetail) #辽宁峨眉山 7 # # print(author_obj.authorDetail.telephone) #444 8 # #2 反向查询 9 # #2 查询一下这个444电话号是谁的. 10 # author_detail_obj = models.AuthorDetail.objects.get(telephone='444') 11 # print(author_detail_obj.author) #崔老师 12 # print(author_detail_obj.author.name) #崔老师 13 14 15 ''' 正向查询:Authorobj.authorDetail,对象.关联属性名称 16 Author----------------------------------->AuthorDetail 17 <----------------------------------- 18 反向查询:AuthorDetailobj.author ,对象.小写类名 19 ''' 20 21 # 一对多 22 # 查询一下李帅的床头故事这本书的出版社是哪个 23 # 正向查询 24 book_obj = models.Book.objects.get(title='李帅的床头故事') 25 print(book_obj.publishs) #B哥出版社 26 print(book_obj.publishs.name) #B哥出版社 27 28 # B哥出版社出版社出版了哪些书 29 # 反向查询 30 pub_obj = models.Publish.objects.get(name='B哥出版社') 31 print(pub_obj.book_set.all()) #<QuerySet [<Book: 李帅的床头故事>, <Book: 李帅的床头故事2>]> 32 33 ''' 正向查询 book_obj.publishs 对象.属性 34 Book ---------------------------------------------> Publish 35 <---------------------------------------------- 36 反向查询 publish_obj.book_set.all() 对象.表名小写_set 37 ''' 38 39 # 多对多 40 # 李帅的床头故事这本书是谁写的 41 # 正向查询 42 book_obj = models.Book.objects.get(title='李帅的床头故事') 43 print(book_obj.authors.all()) 44 # 高杰写了哪些书 45 author_obj = models.Author.objects.get(name='高杰') 46 print(author_obj.book_set.all()) 47 48 ''' 正向查询 book_obj.authors.all() 对象.属性 49 Book ---------------------------------------------> Author 50 <---------------------------------------------- 51 反向查询 author_obj.book_set.all() 对象.表名小写_set 52 '''
基于双下划綫的跨表查询 -- 连表 join
正向查询和反向查询
1 #查询 2 # 一对一 3 # 1. 查询崔老师的电话号 4 # 方式1 正向查询 5 # obj = models.Author.objects.filter(name='崔老师').values('authorDetail__telephone') 6 # print(obj) #<QuerySet [{'authorDetail__telephone': '444'}]> 7 # 方式2 反向查询 8 # obj = models.AuthorDetail.objects.filter(author__name='崔老师').values('telephone','author__age') 9 # print(obj) #<QuerySet [{'telephone': '444'}]> 10 # 2. 哪个老师的电话是444 11 # 正向 12 # obj = models.Author.objects.filter(authorDetail__telephone='444').values('name') 13 # print(obj) 14 # 反向 15 # obj = models.AuthorDetail.objects.filter(telephone='444').values('author__name') 16 # print(obj) 17 18 # 一对多 19 # 查询一下李帅的床头故事这本书的出版社是哪个 20 # obj = models.Book.objects.filter(title='李帅的床头故事').values('publishs__name') 21 # print(obj) #<QuerySet [{'publishs__name': 'B哥出版社'}]> 22 23 # obj = models.Publish.objects.filter(book__title='李帅的床头故事').values('name') 24 # obj = models.Publish.objects.filter(xx__title='李帅的床头故事').values('name') 25 # print(obj) 26 27 # B哥出版社出版社出版了哪些书 28 # obj = models.Publish.objects.filter(name='B哥出版社').values('book__title') 29 # print(obj) #<QuerySet [{'book__title': '李帅的床头故事'}, {'book__title': '李帅的床头故事2'}]> 30 31 # obj = models.Book.objects.filter(publishs__name='B哥出版社').values('title') 32 # print(obj) #<QuerySet [{'title': '李帅的床头故事'}, {'title': '李帅的床头故事2'}]> 33 34 # 李帅的床头故事这本书是谁写的 35 # obj = models.Book.objects.filter(title='李帅的床头故事').values('authors__name') 36 # print(obj) 37 # obj = models.Author.objects.filter(book__title='李帅的床头故事').values('name') 38 # print(obj) #<QuerySet [{'name': '高杰'}, {'name': '崔老师'}]> 39 40 #高杰写了哪些书 41 # obj = models.Book.objects.filter(authors__name='高杰').values('title') 42 # print(obj) 43 # obj = models.Author.objects.filter(name='高杰').values('book__title') 44 # print(obj) 45 46 #进阶的 47 # B哥出版社 出版的书的名称以及作者的名字 48 # obj = models.Book.objects.filter(publishs__name='B哥出版社').values('title','authors__name') 49 # print(obj) 50 #<QuerySet [{'title': '李帅的床头故事', 'authors__name': '高杰'}, {'title': '李帅的床头故事', 'authors__name': '崔老师'}, {'title': '李帅的床头故事2', 'authors__name': '崔老师'}, {'title': '李帅的床头故事2', 'authors__name': '王涛'}]> 51 ''' 52 SELECT app01_book.title,app01_author.name from app01_publish INNER JOIN app01_book on app01_publish.id=app01_book.publishs_id 53 INNER JOIN app01_book_authors on app01_book.nid = app01_book_authors.book_id INNER JOIN app01_author 54 ON app01_author.id = app01_book_authors.author_id where app01_publish.name='B哥出版社'; 55 :param request: 56 :return: 57 ''' 58 59 # obj = models.Publish.objects.filter(name='B哥出版社').values('book__title','book__authors__name') 60 # print(obj) 61 62 # obj = models.Author.objects.filter(book__publishs__name='B哥出版社').values('name','book__title') 63 # print(obj) 64 65 # authorDetail author book publish 66 # 手机号以4开头的作者出版过的所有书籍名称以及出版社名称 67 # ret = models.AuthorDetail.objects.filter(telephone__startswith='4').values('author__book__title','author__book__publishs__name') 68 # print(ret) 69 #QuerySet [{'author__book__title': '李帅的床头故事', 'author__book__publishs__name': 'B哥出版社'}, {'author__book__title': '李帅的床头故事2', 'author__book__publishs__name': 'B哥出版社'}]> 70 71 72 #查询一下B哥出版社出版了哪些书 73 # obj = models.Publish.objects.filter(name='B哥出版社').first() 74 # print(obj.xx.all())
聚合
1 from django.db.models import Avg,Max,Min,Sum,Count 2 3 # 计算所有图书的平均价格 4 # obj = models.Book.objects.all().aggregate(a=Avg('price'),m=Max('price')) #aggregate()是QuerySet 的一个终止子句,得到的是个字典. 5 # print(obj['m'] - 2) #{'price__avg': 2.833333}