在Django中有相当多的操作是通过双下划线与动作连接起来使用,为了以后更加方便的查找和使用,现在总结以下Django中基本的双下划线操作

  比较符:大于--gt  小于--lt 等于--eq  大于等于--gte 小于等于--lte

  

models.Example.objects.filter(id__gt=1)              # 获取id大于1的值
models.Example.objects.filter(id__gte=1) # 获取id大于等于1的值
models.Example.objects.filter(id__lt=10) # 获取id小于10的值
models.Example.objects.filter(id__lte=10) # 获取id小于10的值
models.Example.objects.filter(id__lt=10, id__gt=1) # 获取id大于1 且 小于10的值

  范围操作符:

包含-- in   在范围内--range

models.Example.objects.filter(id__in=[11, 22, 33])   # 获取id等于11、22、33的数据
models.Example.objects.exclude(id__in=[11, 22, 33]) # not in 实际上是exclude的函数生效

包括--contain

models.Example.objects.filter(name__contains="ven")
models.Example.objects.filter(name__icontains="ven") # icontains大小写不敏感
models.Example.objects.exclude(name__icontains="ven")

在范围内--range

models.Example.objects.filter(id__range=[1, 2])   # 范围bettwen 1 and 2

  匹配操作符

为空--isnull

Entry.objects.filter(pub_date__isnull=True)

字符匹配:startswith,istartswith, endswith, iendswith,  ‘i代表大小写不敏感’

  类操作符:

对某一类进行排序--order by

models.Example.objects.filter(name='seven').order_by('id')    # asc 升序
models.Example.objects.filter(name='seven').order_by('-id') # desc降序

对某一类进行归类--group by

from django.db.models import Count, Min, Max, Sum
models.Example.objects.filter(c1=1).values('id').annotate(c=Count('num'))
SELECT "app01_Example"."id", COUNT("app01_Example"."num") AS "c" FROM "app01_Example" WHERE "app01_Example"."c1" = 1 GROUP BY "app01_Example"."id"

正则匹配 regex iregex(不区分大小写)

Entry.objects.get(title__regex=r'^(An?|The) +')
Entry.objects.get(title__iregex=r'^(an?|the) +')

日期相关 date year month day week_day hour minute second

Entry.objects.filter(pub_date__date=datetime.date(2017, 1, 1))
Entry.objects.filter(pub_date__date__gt=datetime.date(2017, 1, 1)) Entry.objects.filter(pub_date__year=2017)
Entry.objects.filter(pub_date__year__gte=2017) Entry.objects.filter(pub_date__month=12)
Entry.objects.filter(pub_date__month__gte=6) Entry.objects.filter(pub_date__day=3)
Entry.objects.filter(pub_date__day__gte=3) Entry.objects.filter(pub_date__week_day=2)
Entry.objects.filter(pub_date__week_day__gte=2) Event.objects.filter(timestamp__hour=23)
Event.objects.filter(time__hour=5)
Event.objects.filter(timestamp__hour__gte=12) Event.objects.filter(timestamp__minute=29)
Event.objects.filter(time__minute=46)
Event.objects.filter(timestamp__minute__gte=29) Event.objects.filter(timestamp__second=31)
Event.objects.filter(time__second=2)
Event.objects.filter(timestamp__second__gte=31)

 

最新文章

  1. linux文件权限查看及修改(实用)
  2. 使用JPype实现Python调用JAVA程序
  3. Java数据库连接池封装与用法
  4. RTX51 Tiny实时操作系统学习笔记—初识RTX51 Tiny
  5. 我的vi/vim配置文件
  6. Weex和React Native框架对比与选择
  7. Vue 组件(component)之 精美的日历
  8. 一些比较隐秘的OJ的网址
  9. Python加密保护-对可执行的exe进行保护
  10. hsy单词
  11. vue data中调用图片的相对路径
  12. linux下yum安装最新稳定版nginx
  13. c#dev tabcontrol 切换页面时注意的问题
  14. CCBAnimationManager
  15. P2260 [清华集训2012]模积和
  16. spring与shiro的集成
  17. angular学习笔记系列一
  18. Python全栈day18(三元运算,列表解析,生成器表达式)
  19. 使用selenium
  20. Linux下修改时间

热门文章

  1. css实现隐藏多余溢出文字并显示省略号
  2. centos/linux下的安装Tomcat
  3. mac清除某个端口的占用
  4. js获取某个日期所在周周一的日期
  5. Java集合框架(三)—— List、ArrayList、Vector、Stack
  6. 《Android进阶之光》--Android新特性
  7. linux分析apache日志获取最多访问的前10个IP
  8. java执行多条SQL语句
  9. Hi3531支持2GByte内存
  10. Hybrid APP 架构设计思路