django查詢

大於、大於等於

__gt 大於
__gte 大於等於
 
User.objects.filter(age__gt=10) # 查詢年齡大於10歲的用戶
User.objects.filter(age__gte=10) # 查詢年齡大於等於10歲的用戶

小於、小於等於

__lt 小於
__lte 小於等於
 
User.objects.filter(age__lt=10) # 查詢年齡小於10歲的用戶
User.objects.filter(age__lte=10) # 查詢年齡小於等於10歲的用戶

小於、小於等於

__lt 小於
__lte 小於等於
 
User.objects.filter(age__lt=10) # 查詢年齡小於10歲的用戶
User.objects.filter(age__lte=10) # 查詢年齡小於等於10歲的用戶

在...範圍內

__in
 
#查詢年齡在某一範圍的用戶
User.objects.filter(age__in=[10, 20, 30])

模糊查詢

__exact #精確等於 like 'aaa'
__iexact #精確等於 忽略大小寫 ilike 'aaa'
__contains# 包含 like '%aaa%'
__icontains #包含 忽略大小寫 ilike '%aaa%',但是對於sqlite來說,contains的作用效果等同於icontains。

是否爲空

is null / is not null
User.objects.filter(username__isnull=True) # 查詢用戶名爲空的用戶
User.objects.filter(username__isnull=False) # 查詢用戶名不爲空的用戶

不等於/不包含於

User.objects.filter().excute(age=10) # 查詢年齡不爲10的用戶
User.objects.filter().excute(age__in=[10, 30]) # 查詢年齡不爲在 [10, 30] 的用戶

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章