Python-Django 模型層-多表查詢

多表操做
基於對象的跨表查詢(屢次查詢)

一對一:

-正向查詢按字段
-反向查詢按表名小寫python

一對多:

-正向查詢按字段(正向查詢必定會查出一個來)
-反向查詢按表名小寫_set.all()(返回結果是queryset對象)對象

多對多:

-正向查詢按字段.all()(正向查詢必定會查出多個來)
-反向查詢按表名小寫_set.all()(返回結果是queryset對象)blog

基於雙下劃線的跨表查詢

-在filter和values中均可以作連表操做(也就是均可以寫 __)
-正向查詢按字段
-反向查詢按表名小寫it

不管以誰作基表,沒有效率之分

1 查詢地址爲上海的做者出版的書
#     res=Author_det.objects.filter(address='上海').values('author__book__name')
#     print(res)
# 	2 兩種方式
# 		查找全部書名裏包含紅樓的書
#     res=Book.objects.filter(name__icontains='eg')
#     print(res)
# 		查找出版日期是2017年的書
#     res=Book.objects.filter(pub_date__year='2017')
#     print(res)

# 		查找出版日期是2017年的書名
#     res = Book.objects.filter(pub_date__year='2017').values('name')
#     print(res)
#     books = Book.objects.filter(pub_date__year='2017')
#     for i in books:
#         print(i)

# 		查找價格大於10元的書
#     res = Book.objects.filter(price__gt=10)
#     print(res)

# 		查找價格大於10元的書名和價格
#     res = Book.objects.filter(price__gt=10).values('name','price')
#     print(res)
#     books = Book.objects.filter(price__gt=10)
#     for i in books:
#         print(i.name,i.price)

# 		查找在北京的出版社
#     res = Publisher.objects.filter(addr='北京')
#     print(res)

# 		查找名字以沙河開頭的出版社
#     res = Publisher.objects.filter(name__startswith='沙河')
#     print(res)

# 		查找做者名字裏面帶「小」字的做者
#     res = Author_sim.objects.filter(name__icontains='小')
#     print(res)

# 		查找年齡大於30歲的做者
#     res = Author_sim.objects.filter(age__gt=30)
#     print(res)

# 		查找手機號是155開頭的做者
#     res = Author_sim.objects.filter(author_det__address__startswith='23')
#     print(res)

# 		查找手機號是155開頭的做者的姓名和年齡
#     res = Author_sim.objects.filter(author_det__address__startswith='23').values('name','age')
#     print(res)
#     authors = Author_sim.objects.filter(author_det__address__startswith='23')
#     for i in authors:
#         print(i.name,i.age)

# 		查找書名是「紅樓夢」的書的出版社
#     res = Book.objects.filter(name='紅樓夢').values('publisher__name')
#     print(res)
#     res = Book.objects.filter(name='紅樓夢')
#     for i in res:
#         print(i.publisher)


# 		查找書名是「紅樓夢」的書的出版社所在的城市
#     res = Book.objects.filter(name='紅樓夢').values('publisher__addr')
#     print(res)
#     book = Book.objects.filter(name='紅樓夢')
#     publisher=Publisher.objects.filter(book=book)
#     for i in publisher:
#         print(i.addr)


    # 		查找書名是「紅樓夢」的書的出版社的名稱
    # res = Book.objects.filter(name='紅樓夢').values('publisher__name')
    # print(res)
    # book = Book.objects.filter(name='紅樓夢')
    # publisher=Publisher.objects.filter(book=book)
    # for i in publisher:
    #     print(i.name)

# 		查找書名是「紅樓夢」的書的全部做者
#     res = Book.objects.filter(name='紅樓夢').values('authors__name')
#     print(res)
#     book = Book.objects.filter(name='紅樓夢')
#     authors=Author_sim.objects.filter(book=book)
#     for i in authors:
#         print(i.name)

# 		查找書名是「紅樓夢」的書的做者的年齡
#     res = Book.objects.filter(name='紅樓夢').values('authors__age')
#     print(res)
#     book = Book.objects.filter(name='紅樓夢')
#     authors=Author_sim.objects.filter(book=book)
#     for i in authors:
#         print(i.age)

# 		查找書名是「紅樓夢」的書的做者的手機號碼
#     res = Book.objects.filter(name='紅樓夢').values('authors__author_det__address')
#     print(res)
#     book = Book.objects.filter(name='紅樓夢')
#     authors = Author_sim.objects.filter(book=book)
#     for i in authors:
#         authors_det = Author_det.objects.filter(author_id=i.id)
#         for i in authors_det:
#             print(i.address)


# 		查找書名是「紅樓夢」的書的做者的地址
#     res = Book.objects.filter(name='紅樓夢').values('authors__author_det__address')
#     print(res)
#     book = Book.objects.filter(name='紅樓夢')
#     author_sim = Author_sim.objects.filter(book=book)
#     for i in author_sim:
#         author_det = Author_det.objects.filter(id=i.id)
#         for i in author_det:
#             print(i.address)

# 		查找書名是「紅樓夢」的書的做者的郵箱
#     res = Book.objects.filter(name='紅樓夢').values('authors__age')
#     print(res)
#     book = Book.objects.filter(name='紅樓夢')
#     author = Author_sim.objects.filter(book=book)
#     for i in author:
#         print(i.age)

  

TTL

 

 

 

基於對象的跨表查詢
-子查詢,多條查詢
-正向:關聯字段在當前表中
-反向:關聯不在當前表中
-正向查詢:按字段
-反向查詢:(具體反向查詢按什麼字段?默認按這種狀況,不建議你修改,related_query_name,寫在關聯字段上)
-一對一:按表名小寫
-一對多:按表名小寫_set.all()
-多對多:按表名小寫_set.all()

-問題:book.authors.author_detail 這個是錯誤的
-book.authors.author_detail.all().author_detail 這個是錯誤的
authors=book.authors.all().
for author in authors:
author.author_detail
-重點:基於對象的跨表查詢,必須用表模型的對象查詢,不能用queryset對象或者其它的對象
基於雙下劃線的跨表查找
-正向:按字段
-反向:按表名小寫(默認狀況下,能夠用:related_name來修改,寫在關聯字段上)
-filter中能夠跨表,values中也能夠跨表
-練習:查找書名是「紅樓夢」的書的出版社的名稱
-基於雙下劃線
-ret=models.Book.objects.filter(name='紅樓夢').values('publish__name')
-ret=models.Publish.objects.filter(book__name='紅樓夢').values('name')class

相關文章
相關標籤/搜索