# 獲取個數
## 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,
# 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'))
# 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)
# day
## entry.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)
Django基礎010 ORM操作
orm返回的資料有兩種,queryset,object 1.queryset支援鏈式程式設計,可以在all 後面繼續.方法 teachers models.teacher.objects.all tea name teachers.values name 過濾字段,獲取我們希望獲取的字段 count...
django中ORM的事務操作
在django的orm中,有兩種使用事務的方式 注意,mysql需要是innodb引擎 from django.db import transaction transaction.atomic def viewfunc request 下面的 在乙個事務中執行,一但出現異常,整個函式中所有的資料庫操...
django中orm的簡單操作
from django.db import models create your models here.from django.db import models create your models here.class author models.model nid models.autofie...