查詢API
(1) all() :查詢所有結果 調用者:objects管理器 返回queryset
ret=Book.objects.all() print(ret) # <QuerySet [<Book: GO>, <Book: linux>, <Book: 北京折疊>, <Book: 三體>, <Book: 追風箏的人>, <Book: 亂世佳人>]
(2) filter() : 它包含了與所給篩選條件相匹配的對象 調用者:objects管理器 返回queryset
# filter 方法:返回值queryset ret=Book.objects.filter(title="linux",price=111) print(ret) # <QuerySet [<Book: linux>]>
(3) get方法(): 返回與所給篩選條件相匹配的對象,返回結果有且只有一 調用者:objects管理器 返回查詢到model對象 (注意:查詢結果有且只有一個才執行)
# get方法:返回查詢到model對象 ret=Book.objects.get(price=111) ret=Book.objects.get(title="linux") print(ret.title) # linux
(4) first(),last()方法: 返回第一條記錄和返回最后一條記錄 調用者:queryset 返回model對象
# first() last()方法:queryset調用 返回model對象 fbook=Book.objects.all()[0] fbook=Book.objects.all().first() lbook=Book.objects.all().last()
(5) exclude(): 它包含了與所給篩選條件不匹配的對象 調用者:objects管理器 返回queryset
#exclude:返回值一個queryset ret=Book.objects.exclude(price=111) print(ret)
(6) order_by(): 對查詢結果排序 由queryset對象調用,返回值是queryset
# order_by:排序 由queryset對象調用,返回值是queryset ret=Book.objects.all().order_by("-price","-nid").first() print(ret)
(7) count : 數數 由queryset對象調用 返回int
# count :數數 :由queryset對象調用 返回int ret=Book.objects.all().count() print(ret)
(8) reverse(): 對查詢結果反向排序 由queryset對象調用,返回值是queryset
# reverse():由queryset對象調用,返回值是queryset Book.objects.all().order_by("price").reverse()
(9) exists(): 如果QuerySet包含數據,就返回True,否則返回False 由queryset對象調用 返回值布爾值
# exists: 由queryset對象調用 返回值布爾值 is_exist=Book.objects.all().exists() if is_exist: print("OK")
(10)values()方法: 由queryset對象調用,返回值是queryset 一個可迭代的字典序列
# values方法:由queryset對象調用,返回值是queryset ret=Book.objects.all().values("title","price") # queryset [{"title":"linux"},{"title":"python"},...] print(ret) # <QuerySet [{'title': 'GO'}, {'title': 'linux'}, {'title': '北京折疊'}, {'title': '三體'}, {'title': '追風箏的人'}, {'title': '亂世佳人'}]> ''' ret=[] for obj in Book.objects.all(): temp={ "title":obj.title "price":obj.price } ret.append(temp) '''
(11)values_list():由queryset對象調用,返回值是queryset 一個元組序列
# values_list:由queryset對象調用,返回值是queryset ret=Book.objects.all().values_list("title","price") print(ret) # <QuerySet [('GO',), ('linux',), ('北京折疊',), ('三體',), ('追風箏的人',), ('亂世佳人',)]>
(12)distinct(): 從返回結果中剔除重復紀錄 由queryset對象調用,返回值是queryset
#distinct: 由queryset對象調用,返回值是queryset ret=Book.objects.all().values("title").distinct() print(ret)
基於雙下划線的模糊查詢
in是三者之中的任意一個 Book.objects.filter(price__in=[100,200,300]) gt是大於 Book.objects.filter(price__gt=100) it是小於 Book.objects.filter(price__lt=100) range是在這個數之間 Book.objects.filter(price__range=[100,200]) contains是只要包含在其中 Book.objects.filter(title__contains="python") icontains是不區分大小寫只要包含在其中 Book.objects.filter(title__icontains="python") startswith是以"py"開頭的 Book.objects.filter(title__startswith="py") 時間 Book.objects.filter(pub_date__year=2012)
#查詢價格大於200的書籍 ret=Book.objects.filter(price__gte=200) print(ret) # 查詢書籍名稱以py開頭的所有的書籍名稱 ret=Book.objects.filter(title__istartswith="py").values("title") ret=Book.objects.filter(title__contains="p").values("title") print(ret) # <QuerySet [{'title': 'pycharm'}, {'title': 'python'}]> # 查詢2017年7月份的所有的書籍 ret=Book.objects.filter(pub_date__year=2017,pub_date__month=7) print(ret)
