Django models字段查询谓词表
| 谓词 | 含义 | 示例 | 等价SQL语句 |
| exact | 精确等于 | Comment.objects.filter(id__exact=14) | select * from Comment where id=14 |
| iexact | 大小写不敏感的等于 | Comment.objects.filter(headline__iexact=’I like this’) | select * from Comment where upper(headline)=’I LIKE THIS’ |
| contains | 模糊匹配 | Comment.objects.filter(headline__contains=’good’) | select * from Comment where headline like “%good%” |
| in | 包含 | Comment.objects.filter(id__in=[1,5,9]) | select * from Comment where id in (1,5,9) |
| gt | 大于 | Comment.objects.filter(n_visits__gt=30) | select * from Comment where n_visits>30 |
| gte | 大于等于 | Comment.objects.filter(n_visits__gte=30) | select * from COmment where n_visits>=30 |
| lt | 小于 | ||
| lte | 小于等于 | ||
| startswith | 以…开头 | Comment.objects.filter(body_text__startswith=”Hello”) | select * from Comment where body_text like ‘Hello%’ |
| endswith | 以…结尾 | ||
| range | 在…范围内 | start_date=datetime.date(2015,1,1) end_date=datetime.date(2015.2.1) Comment.objects.filter( pub_date__range=(start_date,end_date) ) |
select * from Comment where pub_date between ‘2015-1-1’ and ‘2015-2-1’ |
| year | 年 | Comment.objects.filter( pub_date__year=2015 ) |
select * from Comment where pub_date between ‘2015-1-1 0:0:0’ and ‘2015-12-31 23:59:59’ |
| month | 月 | ||
| day | 日 | ||
| week_day | 星期几 | ||
| isnull | 是否为空 | Comment.objects.filter( pub_date__isnull=True ) |
select * from Comment where pub_date is NULL |
filter(**kwargs): 返回符合筛选条件的数据集
exclude(**kwargs): 返回不符合筛选条件的数据集
Comment.objects.filter(pub_date__year=2015)
多个filter和exclude可以链接在一起查询
Comment.objects.filter(pub_date__year=2015).exclude(pub_date__month=1).exclude(n_visits__exact=0)
get() :查询单条记录,注意没有查询到数据的时候会报错
Comment.objects.get(id_exact=1)
all(): 查询所有数据
Comment.objects.all()
Comment.objects.all()[:10]
Comment.objects.all[10:20]
order_by(): 排序
Comment.objects.order_by('headline')
Django models字段查询谓词表的更多相关文章
- django models的点查询/跨表查询/双下划线查询
django models 在日常的编程中,我们需要建立数据库模型 而往往会用到表与表之间的关系,这就比单表取数据要复杂一些 在多表之间发生关系的情形下,我们如何利用models提供的API的特性获得 ...
- Django模型层之字段查询参数及聚合函数
该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. 字段查询是指如何指定SQL WHERE子句的 ...
- django字段查询参数及聚合函数
字段查询是指如何指定SQL WHERE子句的内容.它们用作QuerySet的filter(), exclude()和get()方法的关键字参数. 默认查找类型为exact. 下表列出了所有的字段查询参 ...
- Django开发之路 二(django的models表查询)
django的models表查询 一.单表查询 (1) all(): 查询所有结果 # 返回的QuerySet类型 (2) filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 #返 ...
- Django orm进阶查询(聚合、分组、F查询、Q查询)、常见字段、查询优化及事务操作
Django orm进阶查询(聚合.分组.F查询.Q查询).常见字段.查询优化及事务操作 聚合查询 记住用到关键字aggregate然后还有几个常用的聚合函数就好了 from django.db.mo ...
- Django---Django的ORM的一对多操作(外键操作),ORM的多对多操作(关系管理对象),ORM的分组聚合,ORM的F字段查询和Q字段条件查询,Django的事务操作,额外(Django的终端打印SQL语句,脚本调试)
Django---Django的ORM的一对多操作(外键操作),ORM的多对多操作(关系管理对象),ORM的分组聚合,ORM的F字段查询和Q字段条件查询,Django的事务操作,额外(Django的终 ...
- Django框架(六):模型(二) 字段查询、查询集
1. 字段查询 通过模型类.objects属性可以调用如下函数,实现对模型类对应的数据表的查询. 函数名 功能 返回值 说明 get 返回表中满足条件的一条且只能有一条数据. 返回值是一个模型类对象. ...
- Django models .all .values .values_list 几种数据查询结果的对比
Django models .all .values .values_list 几种数据查询结果的对比
- Django对DateTimeField时间字段查询QuerySet为空的解决方案
今天在用的Django中的filter()方法对DateTimeField字段查询时碰到了问题,抓耳挠腮一下午,终于解决了,我觉得花了一下午的时间怎么着也得记录下吧(无语)...... 问题描述 : ...
随机推荐
- SpringBoot整合Jdbc
(1).添加相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...
- SpringBoot使用其他的Servlet容器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- 【转载】apache log配置 按日期写日志
指定apache日志每天生成一个文件 Linux系统配置方法 在apache的配置文件httpd.conf中找到 代码如下1 ErrorLog logs/error_log CustomLog log ...
- 安装installshield问题
install designer中 general information 选择setup languages shortcuts编辑 开始 中显示目录 文件路径 C:\Program Files ...
- 019_nginx upstream中keepalive参数
一. TCP/IP State=>SYN_RECV,LISTEN,TIME_WAIT,ESTABLISHED,STREAM,CONNECTED,CLOSING (1)前端Nginx大量报no l ...
- ubuntu系统下Python虚拟环境的安装和使用
ubuntu系统下Python虚拟环境的安装和使用 前言:进行python项目开发的时候,由于不同的项目需要使用不同的资源包和相关的配置,因此创建多个python虚拟环境,在虚拟环境下开 ...
- JAVA中各种日期表示字母
字母 日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Month July; Jul; 07 w 年中的周数 Number 27 W ...
- 来自工程师的8项Web性能提升建议
在互联网盛行的今天,越来越多的在线用户希望得到安全可靠并且快速的访问体验.针对Web网页过于膨胀以及第三脚本蚕食流量等问题,Radware向网站运营人员提出以下改进建议,帮助他们为用户提供最快最优质的 ...
- 测试开发之前端——No9.HTML5中的视频/音频
HTML5 视频和音频的 DOM 参考手册 HTML5 DOM 为 <audio> 和 <video> 元素提供了方法.属性和事件. 这些方法.属性和事件允许您使用 JavaS ...
- python 全栈开发,Day141(flask之应用上下文,SQLAlchemy)
一.flask之应用上下文 由于时间关系,详细过程略... 草稿图 参考链接: http://www.cnblogs.com/zhaopanpan/p/9457343.html 总结: 上下文管理(应 ...