Django中ORM的操作方法

大於,大於等於

__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歲的用戶

in

__in      查詢年齡在某一範圍的用戶

User.objects.filter(age__in=[10, 20, 30])

like

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

is null / is not null

__isnull          判空
User.objects.filter(username__isnull=True)  // 查詢用戶名爲空的用戶
User.objects.filter(username__isnull=False) // 查詢用戶名不爲空的用戶

不等於/不包含於

User.objects.filter().exclude(age=10)  // 查詢年齡不爲10的用戶
User.objects.filter().exclude(age__in=[10, 20]) // 查詢年齡不爲在 [10, 20] 的用戶


發佈了16 篇原創文章 · 獲贊 13 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章