1. 程式人生 > >數據查詢 | Django開發

數據查詢 | Django開發

val con __str__ 需要 獲取 aggregate 單個 port 列表

        1.獲取所有對象
            object1 = 表名.objects.all()
            # bookinfo = BookInfo.objects.all()
            # 可以通過切片選擇範圍內對象(限制查詢集合)
                object2 = 表名.objects.all()[1,3]
        ================================================================================
        2.過濾器(查詢集的方法)
            
2.1 條件查詢 1.filter() # 滿足查詢,始終返回一個查詢集,列表 2.exclude() # 不滿足查詢 3.order_by() 4.values() 5.all() 2.2 語法規則: # 表名.objects.filter(字段名__運算符判斷=‘值‘) 2.3 運算符 1.exact # 精確匹配 2.contains #
包含 3.startswith # 以指定值開頭 4.endswith # 以指定值結束 5.isnull # 不為空 6.in # 範圍內查詢 7.gt/gte/lt/lte # 運算符前加上i,不區分大小寫; 2.4 例子 # filter()過濾器 # 精確查詢 bookinfo_3 = BookInfo.objects.filter(id__exact=1)
print(bookinfo_3) # 包含查詢 bookinfo4 = BookInfo.objects.filter(book_title__contains=) print(bookinfo4) # 首字查詢 bookinfo5 = BookInfo.objects.filter(book_title__startswith=) print(bookinfo5) # 尾字查詢 bookinfo6 = BookInfo.objects.filter(book_title__endswith=) print(bookinfo6) # 查看不為空的字段值 bookinfo7 = BookInfo.objects.filter(book_title__isnull=False) print(bookinfo7) # 查詢指定編號範圍的字段值 bookinfo8 = BookInfo.objects.filter(pk__in=[1,3]) print(bookinfo8) # 查詢大小範圍內的字段值 bookinfo9 = BookInfo.objects.filter(id__gt=3) print(bookinfo9) # exclude()過濾器 bookinfo10 = BookInfo.objects.exclude(id__gt=3) print(bookinfo10) ================================================================================ 3.返回單個值的方法 3.1 get 獲取單一對象 bookinfo11 = BookInfo.objects.get(book_title = 天龍八部) print(bookinfo11) # get如果查詢的不僅一個對象,會觸發MultipleObjectsReturned異常 bookinfo12 = BookInfo.objects.get(id__gt=2) print(bookinfo12) # get如果查詢不到對象,會觸發DoesNotExist異常 bookinfo13 = BookInfo.objects.get(book_title = 天龍七部) print(bookinfo13) 3.2 count() 3.3 first() 3.4 last() 3.5 exists() ================================================================================ 4.F對象和Q對象 # 需要導入模塊中的對象 # from django.db.models import F, Q 4.1 F對象 # 實現兩個屬性之間的比較 bookinfo14 = BookInfo.objects.filter(book_read__gte=F(book_comment)) print(bookinfo14) # 可以在F()對象上使用算數運算 bookinfo15 = BookInfo.objects.filter(book_read__gte=F(book_comment)*2) print(bookinfo15) 4.2 Q對象(query查詢) # 構造查詢對象 -- 邏輯與:多個過濾器逐個調用 bookinfo16 = BookInfo.objects.filter(book_read__lte=40, id__lte=3) print(bookinfo16) bookinfo17 = BookInfo.objects.filter(book_read__lte=40).filter(id__lte=3) print(bookinfo17) -- 利用Q對象實現,或與非 -- 基本語法 Q(屬性名__運算符=值) # &表示邏輯與,|表示邏輯或 bookinfo18 = BookInfo.objects.filter(Q(book_read__lte=40) & Q(id__lte=3)) print(bookinfo18) bookinfo19 = BookInfo.objects.filter(Q(book_read__lte=40) | Q(id__lte=3)) print(bookinfo19) ================================================================================ 5.聚合函數 # 需要導入模塊 # from django.db.models import * # 表名.objects.aggregate(Sum(字段名)) 1.Avg 2.Count # 查詢總數的時候一般不使用; # BookInfo.objects.count() 替代; 3.Max 4.Min 5.Sum bookinfo20 = BookInfo.objects.aggregate(Sum(book_read)) print(bookinfo20) ================================================================================ 6.關聯查詢 6.1 一對多,一類 1.獲取單一字段對象 object1 = 表名.objects.get(條件) # book = BookInfo.objects.get(id=1) 2.獲取對應所有所有對象信息 object2 = object1.小寫表名_set.all() # heroes = book.heroinfo_set.all() 6.2 多對一,多類 1.獲取單一字段對象 object1 = 表名.objects.get(條件) # hero = HeroInfo.objects.get(id=1) 2.獲取對應對象信息 object1.字段屬性 # hero.hero_book ================================================================================ 7.自關聯 # 內部的關系字段指向本表的主鍵,這就是自關聯的表結構; # 比如省市查詢的模型 7.1 模型類 class AreaInfo(models.Model): # 地區名稱 area_name = models.CharField(max_length=30) # 父級地區 area_parent = models.ForeignKey(self, null=True, blank=True) def __str__(self): return self.area_name 7.2 測試數據 areas = [("北京", None), ("河北", None), ("海澱區", 1), ("昌平區", 1), ("順義區", 1), ("房山區", 1), ("朝陽區", 1), ("豐臺區", 1), ("石家莊", 2), ("唐山", 2), ("保定", 2), ("邢臺", 2), ("邯鄲", 2), ("秦皇島", 2)] for area in areas: area_info = AreaInfo() area_info.area_name = area[0] area_info.area_parent_id = area[1] area_info.save() 7.3 查詢 AreaInfo.objects.filter(area_parent__area_name=None) AreaInfo.objects.filter(area_parent__area_name="北京") AreaInfo.objects.filter(area_parent__area_name="河北") ================================================================================ 8.結果集排序 8.1 order_by # 升序排列 hero_list = HeroInfo.objects.order_by(id) # 降序排列 hero_list = HeroInfo.objects.order_by(-id) 8.2 如果有需求,讓數據表排序默認為倒序,可以通過元類來實現 class HeroInfo(models.Model): hero_name = models.CharField(max_length=20) class Meta: ordering = [-id] # 此時查詢結果集默認就是根據ID降序排列的結果 hero_list = HeroInfo.objects.all() ================================================================================

數據查詢 | Django開發