大於、大於等於
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)//查詢狀態為空