1.新增記錄:
# 一對多的新增方式: #pub_obj=Publish.objects.filter(name="橙子出版社").first() # book=Book.objects.create( # title="python", # price=120, # pub_date="2012-12-12", # publish_id=1, # #publish=pub_obj # # ) # 多對多的新增方式: # 方式1 # alex=Author.objects.filter(name="alex").first() # egon=Author.objects.filter(name="egon").first() # book.authors.add(alex,egon) # 方式2: # book.authors.add(1,2) # 方式3: # book.authors.add(*[1,2])
2.解除在繫結
###### 解除再繫結 book = Book.objects.filter(nid=3).first() book.authors.clear() book.authors.add(1) book.authors.set(1)
3.基於物件的跨表查詢
######################### 基於物件的跨表查詢 ############################# ####### 一對多 ########## ''' 正向查詢:關聯屬性所在的表查詢關聯表記錄 反向查詢 ----正向查詢按欄位:book.publish Book------------------------------------>Publish <----------------------------------- 反向查詢表名小寫_set.all():pub_obj.book_set.all() ''' # 1 查詢python這本書出版社的名字和郵箱 # book=Book.objects.filter(title="python").first() # pub_obj=Publish.objects.filter(nid=book.publish_id).first() # print(pub_obj.name) # print(pub_obj.email) ######################## # book = Book.objects.filter(title="python").first() # print(book.publish) # 與book這本書關聯出版社物件 # print(book.publish.name) # 與book這本書關聯出版社物件 # print(book.publish.email) # 與book這本書關聯出版社物件 # 2 查詢蘋果出版社出版的所有的書籍的名稱 # pub_obj=Publish.objects.get(name="蘋果出版社") # print(pub_obj.book_set.all()) # queryset # print(pub_obj.book_set.all().values("title")) # queryset ####### 多對多 ########## ''' 正向查詢按欄位 book.authors.all() Book -------------------------------------->Author <-------------------------------------- 反向查詢按表名小寫_set.all(): alex.book_set.all() ''' # 查詢python這本書籍的作者的年齡 book=Book.objects.filter(title="python").first() ret=book.authors.all().values("age") # 與這本書關聯的左右作者的queryset的集合 print(ret) # #查詢alex出版過的所有的書籍名稱 # alex=Author.objects.filter(name="alex").first() # print(alex.book_set.all()) ####### 一對一 ########## ''' 正常查詢安欄位:alex.ad Author -----------------------------------------> AuthorDetail <------------------------------------------ 反向查詢按表名小寫 ad.author ''' # 查詢alex的手機號 # alex = Author.objects.filter(name="alex").first() # print(alex.ad.tel) # # 查詢手機號為110的作者的名字 # ad=AuthorDetail.objects.filter(tel=110).first() # print(ad.author.name)
4.基於雙下劃線的跨表查詢
################基於雙下劃線的跨表查詢(基於join實現的)################ # KEY:正向查詢按欄位,反向查詢按表明小寫 # 1 查詢python這本書出版社的名字 # ret=Book.objects.filter(title="python").values("price") # ret=Book.objects.filter(title="python").values("publish__name") # print(ret) # ret=Publish.objects.filter(book__title="python").values("name") # print(ret) # 2 查詢蘋果出版社出版的所有的書籍的名稱 # ret=Publish.objects.filter(name="蘋果出版社").values("book__title") # ret=Book.objects.filter(publish__name="蘋果出版社") # 3 查詢python這本書籍的作者的年齡 # ret=Book.objects.filter(title="python").values("authors__age") # print(ret) # ret=Author.objects.filter(book__title="python").values("age") # # # 4 查詢alex出版過的所有的書籍名稱 # ret1=Book.objects.filter(authors__name="alex").values("title") # ret2=Author.objects.filter(name="alex").values("book__title") # print(ret1,ret2) # 5 查詢alex的手機號 # ret=Author.objects.filter(name="alex").values("ad__tel") # ret=AuthorDetail.objects.filter(author__name="alex").values("tel") # 6 查詢手機號為110的作者的名字 # ret=AuthorDetail.objects.filter(tel=110).values("author__name") # ret=Author.objects.filter(ad__tel=110).values("name") ########### 連續跨表 ############### # 查詢蘋果出版社出版過的所有書籍的名字以及作者的姓名 # ret=Publish.objects.filter(name="蘋果出版社").values("book__title","book__authors__name") # ret=Book.objects.filter(publish__name="蘋果出版社").values("title","authors__name") # ret=Book.objects.filter(publish__name="蘋果出版社").values("title","authors__name") # print(ret) # 手機號以110開頭的作者出版過的所有書籍名稱以及出版社名稱 # 方式1: # ret=Author.objects.filter(ad__tel__startswith=110).values_list("book__title","book__publish__name") # print(ret) # 方式2: # ret=AuthorDetail.objects.filter(tel__startswith=110).values("author__book__title","author__book__publish__name") # # # 方式3: # ret=Book.objects.filter(authors__ad__tel__startswith=110).values("title","publish__name") ################ 聚合 分組 ################ ''' emp id name dep pro salary 1 alex 教學部 山東 1000 2 mjj 教學部 山東 3000 3 林海峰 保安部 山東 5000 4 peiqi 人事部 河北 10000 select Count(id) from emp; select AVG(salary) from emp; select dep,AVG(salary) from emp group by dep select pro,Count(1) from emp group by pro ''' # 聚合 # 查詢所有書籍的平均價格 from django.db.models import Avg,Max,Sum,Min,Count # ret=Book.objects.all().aggregate(priceAvg=Avg("price")) # print(ret) # {'priceAvg': 142.0} # # 查詢所有書籍的個數 # ret=Book.objects.all().aggregate(c=Count(1)) # print(ret) # {'c': 4} # 分組 # 單表分組查詢 # 查詢書籍表每一個出版社id以及對應的書籍個數 # key: annotate()前values哪一個欄位就按哪一個欄位group by # ret=Book.objects.values("publish_id").annotate(c=Count(1)) # print(ret) # # # 查詢每一個部門的名稱以及對應員工的平均薪水 # ret=Emp.objects.values("dep").annotate(avg_salary=Avg("salary")) # print(ret) # [{'dep': '教學部', 'avg_salary': 2500.0}, {'dep': '保潔部', 'avg_salary': 3500.0}, {'dep': '保安部', 'avg_salary': 4000.0}]> # # # 查詢每一個省份的名稱以及對應的員工最大年齡 # # ret=Emp.objects.values("pro").annotate(max_age=Max("age")) # print(ret) # <QuerySet [{'pro': '山東省', 'max_age': 123}, {'pro': '河南省', 'max_age': 23}, {'pro': '河北省', 'max_age': 56}]> # 單表按主鍵分組沒有意義 # Emp.objects.values("id").annotate() # 跨表分組查詢 # ''' # select app01_publish.name,COUNT(1) from app01_book INNER JOIN app01_publish ON app01_book.publish_id=app01_publish.nid # # GROUP BY app01_publish.nid # ''' # 1 查詢每一個出版社的名稱以及對應的書籍平均價格 # 方式1: # ret=Publish.objects.values("name","email").annotate(avg_price=Avg("book__price")) # print(ret) # <QuerySet [{'name': '蘋果出版社', 'avg_price': 117.0}, {'name': '橙子出版社', 'avg_price': 112.0}, {'name': '西瓜出版社', 'avg_price': 222.0}]> # 方式2: # ret=Publish.objects.all().annotate(avg_price=Avg("book__price")).values("name","email","avg_price") # print(ret) # <QuerySet [<Publish: 蘋果出版社>, <Publish: 橙子出版社>, <Publish: 西瓜出版社>]> # 方式3: # ret=Publish.objects.annotate(avg_price=Avg("book__price")).values("name","email","avg_price") # print(ret) # <QuerySet [<Publish: 蘋果出版社>, <Publish: 橙子出版社>, <Publish: 西瓜出版社>]> # 2 查詢每一個作者的名字以及出版的書籍的最高價格 # ret=Author.objects.values("pk","name").annotate(max_price=Max("book__price")) # print(ret) # ret=Author.objects.annotate(maxprice=Max("book__price")).values("name","maxprice") # print(ret) # 3 查詢每一個書籍的名稱以及對應的作者的個數 # ret=Book.objects.values("title").annotate(c=Count("authors")) # print(ret) # <QuerySet [{'title': 'python', 'authors__count': 2}, {'title': 'linux', 'authors__count': 1}, {'title': 'go', 'authors__count': 1}, {'title': 'java', 'authors__count': 0}]> # ret=Book.objects.annotate(c=Count("authors")).values("title","c") # print(ret) # 4 查詢作者數不止一個的書籍名稱以及作者個數 # ret=Book.objects.annotate(c=Count("authors__name")).filter(c__gt=1).values("title","c") # print(ret) # 5 根據一本圖書作者數量的多少對查詢集 QuerySet進行排序 # ret=Book.objects.annotate(c=Count("authors__name")).order_by("c") # 6 統計每一本以py開頭的書籍的名稱以及作者個數 # ret=Book.objects.filter(title__startswith="py").annotate(c=Count("authors__name"))
5.FQ查詢
################ F 與 Q ################ from django.db.models import F,Q # # 查詢評論數大於100的所有的書籍名稱 # ret=Book.objects.filter(comment_count__gt=100).values("title") # # 查詢評論數大於2倍點贊數的所有的書籍名稱 # ret=Book.objects.filter(comment_count__gt=F("poll_count")*2) # print(ret) # 給每一本書籍的價格提升100 # Book.objects.all().update(price=100+F("price")) # 查詢價格大於300或者評論數大於3000的書籍 # ret=Book.objects.filter(price__gt=300,comment_count__gt=3000) # print(ret) # 與 & 或 | 非 ~ # ret = Book.objects.filter(Q(price__gt=300)|~Q(comment_count__gt=3000)) # print(ret) ret = Book.objects.filter(Q(Q(price__gt=300) | ~Q(comment_count__gt=3000))&Q(poll_count__gt=2000)) ret = Book.objects.filter(Q(Q(price__gt=300) | ~Q(comment_count__gt=3000)),poll_count__gt=2000) #ret = Book.objects.filter(poll_count__gt=2000,Q(Q(price__gt=300) | ~Q(comment_count__gt=3000))) print(ret)