一. 常见的三个函数
1. filter
2. exclude
3. get
4. dates(field, kind, order=’ASC’)
(1) field should be the name of a DateField of your model.
(2) kind should be either "year", "month" or "day". Each datetime.date object in the result list is “truncated” to the given type.
• "year" returns a list of all distinct year values for the field.
• "month" returns a list of all distinct year/month values for the field.
• "day" returns a list of all distinct year/month/day values for the field.
(3)order, which defaults to ’ASC’, should be either ’ASC’ or ’DESC’. This specifies how to order the results.
二. 常见的查询方式
方式名 | 大小写敏感 | 说明 |
exact | 是 | 等于 |
iexact | 否 | |
contain | 是 | 包含 |
icontain | 否 | |
in | 无 | 成员 |
range | 无 | 范围 |
gt | 无 | 大于 |
gte | 无 | 大于等于 |
lt | 无 | 小于 |
lte | 无 | 小于等于 |
startswith | 是 | 以...开始 |
istartswith | 否 | |
endswith | 是 | 以...结尾 |
istartswith | 否 | |
year,month,day,week_day,hour,minute,second | 无 | 时间 |
isnull | 无 | 为空 |
regex | 是 | 正则表达式 |
iregex | 否 |
三. 双下划线(__)的作用
1.字段查询类型:field__lookuptype=value
# 查询Blog中create_time < somedate的所有数据Blog.objects.filter(create_time__lt=somedate)
2.外键的扩展:
3.日期的扩展:
# 查询Blog中create_time.year = someyear的数据Blog.objects.filter(create_time__year=someyear)
4.链式查询:
#查询Blog中create_time.year < someyear的所有数据Blog.objects.filter(create_time__year__lt=someyear)
注明:学习资料来自以及