标签:ast not 表结构 field 秦皇岛 als 比较 object 列表
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() ================================================================================
标签:ast not 表结构 field 秦皇岛 als 比较 object 列表
原文地址:http://www.cnblogs.com/pymkl/p/7737473.html