字段查找是指定SQL WHERE子句的核心内容的方式。
它们被指定为QuerySet方法filter()、exclude()和get()的关键字参数。
1、exact:精确查找。如果为比较提供的值为None,则会将其解释为SQL NULL
在MySQL中,数据库表的“排序”设置决定了精确的比较是否区分大小写。这是一个数据库设置,而不是Django设置。
可以将MySQL表配置为使用区分大小写的比较。

2、iexact:不区分大小写的精确查找

3、contains:包含,模糊查询

4、icontains:包含,不区分大小写

5、in:范围查询

也可以判定结果集,子查询

如果将由values()或values_list()生成的QuerySet作为值传递给__in查找,则需要确保只提取结果中的一个字段。

6、gt:大于;
gte:大于等于
lt:小于
lte:小于等于

7、startswith:区分大小写的开始。
istartswith:不区分大小写

8、endswith:区分大小写结尾
iendswith:不区分大小写

9、range:范围
在SQL中,您可以在任何地方使用range——日期、数字甚至字符。

10、date,year,month,day,weak

11、weekday:取一个整数值,表示从1(星期日)到7(星期六)的星期几。

12、quarter:季度。对于日期和日期时间字段,匹配一个“季度”。允许链接额外的字段查找。取1到4之间的整数值,表示该年度的季度。
Entry.objects.filter(pub_date__quarter=2)
13、time:时间

14、hour:小时。对于日期时间和时间字段,一个精确的小时匹配。允许链接额外的字段查找。取0到23之间的整数。
minute:分钟。0-59
second:秒。0-59

15、isnull:是否为空,对应SQL的IS NULL 和IS NOT NULL

16、regex:正则匹配
iregex:不区分大小写的正则表达式

网友评论