[Django学习] Django基础(6)_Field lookups

一. 常见的三个函数

  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)

  

猜你喜欢

转载自www.cnblogs.com/AngryZe/p/9263684.html