django基础之ORM-----查询操作(2)

4.order_by( )

objects_order_by = Book.objects.order_by('price')
print(objects_order_by)
for order in objects_order_by:
    print(order)

        用法:model类名.objects.order_by('-列名','列名' )

        作用:相当于sql语句的order by子句对查询结果根据某个字段选择性的进行排序。默认升序排序,降序列名前面加'-'。

     

此外:使用query方法可以得到对应的sql语句

5.filter(条件)

        用法:model类名.objects.filter(属性名1=值1,属性名2=值2)

        作用:返回包含此条件的所有数据集。返回值QuerySet容器对象,内部存放model实例。

objects_filter = Book.objects.filter(pub='北京出版社')
for book in objects_filter:
    print(book)

6.exclude( ),与filter相反,返回不包含此条件的全部数据集。

7.get(条件)

        用法:model类名.objects.get

        作用:返回满足条件的唯一一条数据。查询结果大于一条数据,会抛出异常。

objects_get = Book.objects.get(price=45)
print(objects_get)

8.字段名__exact

        

book_objects_filter = Book.objects.filter(price__exact=37)
print(objects_get)
#等同于select * from book where price = 37

9.字段名__contains

        包含指定值

q = Book.objects.filter(pub__contains='版')
print(q)
#等同于select * from book where pub like '%版%'

10.字段名__startwith

query_set = Book.objects.filter(title__startswith='C')
print(query_set)
for book in query_set:
    print(book)

11.字段名__endwith

q1 = Book.objects.filter(title__endswith='弃')
print(q1)
for book in q1:
    print(book)

12.字段名__gt : 大于指定值

q2 = Book.objects.filter(price__gt=50)
print(q2)
for book in q2:
    print(book)

13.      字段名__gte:大于等于

14.      字段名__lt:小于

15.       字段名__lte:小于等于

16.       字段名__in:查询数据指定范围内数据

q1 = Book.objects.filter(title__in=['C++入门到放弃'])
print(q1)
for book in q1:
    print(book)

17.       字段名__range:查询在指定区间内数据集

q1 = Book.objects.filter(price__range=(38, 99))
print(q1)
for book in q1:
    print(book)

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值