django ORM雙下劃線彙總

2021-09-28 06:43:41 字數 3535 閱讀 3970

雙下劃線

# 獲取個數

models.tb1.objects.

filter

(name=

'seven'

).count(

)# 大於,小於

models.tb1.objects.

filter

(id__gt=1)

# 獲取id大於1的值

models.tb1.objects.

filter

(id__gte=1)

# 獲取id大於等於1的值

models.tb1.objects.

filter

(id__lt=10)

# 獲取id小於10的值

models.tb1.objects.

filter

(id__lte=10)

# 獲取id小於10的值

models.tb1.objects.

filter

(id__lt=

10, id__gt=1)

# 獲取id大於1 且 小於10的值

# in

models.tb1.objects.

filter

(id__in=[11

,22,33

])# 獲取id等於11、22、33的資料

models.tb1.objects.exclude(id__in=[11

,22,33

])# not in

# isnull

entry.objects.

filter

(pub_date__isnull=

true

)# contains

models.tb1.objects.

filter

(name__contains=

"ven"

)models.tb1.objects.

filter

(name__icontains=

"ven"

)# icontains大小寫不敏感

models.tb1.objects.exclude(name__icontains=

"ven"

)# range

models.tb1.objects.

filter

(id__range=[1

,2])

# 範圍bettwen and

# 其他類似

以什麼開始:startswith,istartswith,

以什麼結尾:endswith, iendswith,

istartswith,iendswith:大小寫是不敏感的

order by

models.tb1.objects.

filter

(name=

'seven'

).order_by(

'id'

)# asc

models.tb1.objects.

filter

(name=

'seven'

).order_by(

'-id'

)# desc

group by

from django.db.models import count, min, max, sum

models.tb1.objects.

filter

(c1=1)

.values(

'id'

).annotate(c=count(

'num'))

."id"

, count(

."num"

."c1"=.

"id"

limit 、offset

models.tb1.objects.

all()[

10:20]

regex正則匹配,iregex 不區分大小寫

entry.objects.get(title__regex=r'^(an?|the) +'

)entry.objects.get(title__iregex=r'^(an?|the) +'

)date

entry.objects.

filter

(pub_date__date=datetime.date(

2005,1

,1))

entry.objects.

filter

(pub_date__date__gt=datetime.date(

2005,1

,1))

year

entry.objects.

filter

(pub_date__year=

2005

)entry.objects.

filter

(pub_date__year__gte=

2005

)month

entry.objects.

filter

(pub_date__month=12)

entry.objects.

filter

(pub_date__month__gte=6)

dayentry.objects.

filter

(pub_date__day=3)

entry.objects.

filter

(pub_date__day__gte=3)

week_day

entry.objects.

filter

(pub_date__week_day=2)

entry.objects.

filter

(pub_date__week_day__gte=2)

hour

event.objects.

filter

(timestamp__hour=23)

event.objects.

filter

(time__hour=5)

event.objects.

filter

(timestamp__hour__gte=12)

minute

event.objects.

filter

(timestamp__minute=29)

event.objects.

filter

(time__minute=46)

event.objects.

filter

(timestamp__minute__gte=29)

second

event.objects.

filter

(timestamp__second=31)

event.objects.

filter

(time__second=2)

event.objects.

filter

(timestamp__second__gte=

31)

Python單下劃線和雙下劃線

python 用下劃線作為變數字首和字尾指定特殊變數。不能用 from moduleimport 匯入 系統定義名字 類中的私有變數名 核心風格 避免用下劃線作為變數名的開始。因為下劃線對直譯器有特殊的意義,而且是內建識別符號所使用的符號,我們建議程式設計師避免用下 劃線作為變數名的開始。一般來講,...

Python self 雙下劃線

class ball def init self,name self.name name ball ball qwe ball.name qwe class ball def setname self,name self.name name defkick self print 我是 s self....

雙下劃線 fun

雙下劃線表示私有 private 型別的成員。只允許定義該方法的類在類內部進行訪同,不能 通過類的例項進行訪問,但是可以通過 類的例項名類名 方式進行訪問。例7.13建立乙個geese類,定義私有屬性neck,並在 init o方法中訪問該屬性,最後生成geese的例項,通過例項名輸出私有屬性 ne...