django基础 --- models过滤条件

  • filter,精确匹配,=
Author.objects.filter(id="1")
#select xxx from author where id="1"
  • exclude,取反!=
Author.objects.exclude(id="1")
#select xxx from author where id != "1"
  • __exact,精确匹配 like "xxx"
Author.objects.filter(name__exact="yang")
#select xxx from author where name like "yang"
  • __iexact,精确匹配,忽略大小写 like "xxx"
Author.objects.filter(name__iexact="YANG")
#select xxx from author where name like "YANG"
  • __contains,模糊匹配,like "%xxx%"
Author.objects.filter(name__contains="yang")
#select xxx from author where name like "%yang%"
  • __icontains,忽略大小写模糊匹配
用法同上
  • __gt,大于
Author.objects.filter(id__gt="1")
#select xxx from author where id > 1
  • __gte,大于等于
用法同上
  • __lt,小于

  • __lte,小于等于

  • __in,在一个list范围内

Author.objects.filter(id__in=[1,2,3,4,5])
#select xxx from author where id in (1,2,3,4,5)
  • __startswith,以xxx开头
Author.objects.filter(name__startswith="y")
#select xxx from author where name like "y%"
  • __istartswith, 以xxx开头,忽略大小写

  • __endswith,以xxx结尾

  • __iendswith,以xxx结尾,忽略大小写

  • __range,在xxx范围内

Author.objects.filter(id__range=[1,10])
#select xxx from author where id >= 1 and id <= 10
  • __year,日期字段的年份
Author.objects.filter(create_date__year="2015")
  • __month 日期字段的月份
  • __day 日期字段的日

你可能感兴趣的:(django基础 --- models过滤条件)