大于、大于等于
1
2
3
4
5
|
__gt 大于
__gte 大于等于
User.objects.filter(age__gt=10) // 查询年龄大于10岁的用户
User.objects.filter(age__gte=10) // 查询年龄大于等于10岁的用户
|
小于、小于等于
1
2
3
4
5
|
__lt 小于
__lte 小于等于
User.objects.filter(age__lt=10) // 查询年龄小于10岁的用户
User.objects.filter(age__lte=10) // 查询年龄小于等于10岁的用户
|
在...范围内
1
2
3
4
|
__in
查询年龄在某一范围的用户
User.objects.filter(age__in=[10, 20, 30])
|
模糊查询
1
2
3
4
|
__exact 精确等于 like 'aaa'
__iexact 精确等于 忽略大小写 ilike 'aaa'
__contains 包含 like '%aaa%'
__icontains 包含 忽略大小写 ilike '%aaa%',但是对于sqlite来说,contains的作用效果等同于icontains。
|
是否为空
1
2
3
|
is
null
/
is
not
null
User.objects.
filter
(username__isnull
=
True
)
/
/
查询用户名为空的用户
User.objects.
filter
(username__isnull
=
False
)
/
/
查询用户名不为空的用户
|
不等于/不包含于
1
2
|
User.objects.filter().excute(age=10) // 查询年龄不为10的用户
User.objects.filter().excute(age__in=[10, 20]) // 查询年龄不为在 [10, 20] 的用户
|
Django使用or条件查询:
from django.db.models import Q
User.objects.filter(Q(state=0) | Q(state=1))
Django各种条件查询关键字:
__exact 精确等于 like ‘aaa’
__iexact 精确等于 忽略大小写 ilike ‘aaa’
__contains 包含 like ‘%aaa%’
__icontains 包含 忽略大小写 ilike ‘%aaa%’,但是对于sqlite来说,contains的作用效果等同于icontains。
__gt 大于
__gte 大于等于
__lt 小于
__lte 小于等于
__in 存在于一个list范围内
__startswith 以…开头
__istartswith 以…开头 忽略大小写
__endswith 以…结尾
__iendswith 以…结尾,忽略大小写
__range 在…范围内
__year 日期字段的年份
__month 日期字段的月份
__day 日期字段的日
__isnull=True/False
User.objects.filter(state__gt=0)//查询状态大于0
User.objects.filter(state__isnull=True)//查询状态为空