在使用python後端開發時,很多人都會遇到使用原生sql還是django自帶的orm進行數據庫的操作好呢?糾結的原因有很多,其中一點就是對orm不熟悉,導致某些查詢可能不知道如何實現,於是采用了原生sql,今天,就讓我們來總結一下常用的orm查詢:
1、返回結果是列表裡套對象:
models.Classes.objects.all() # 相當於select * from classes;
2、返回結果是列表裡套字典形式:
models.Classes.objects.values() # 相當於 select * from classes
models.Classes.objects.values('id','name') # 相當於 select id,name from classes,可指定查詢的字段
3、返回結果是列表裡套元組形式:
models.Classes.objects.values_list() # 相當於 select * from classes
models.Classes.objects.values_list('id','name') # 相當於 select id,name from classes,可指定查詢的字段
models.Classes.objects.values('name').distinct() # select distinct name from classes
models.Classes.objects.values('id','name').distinct() # 將(id,name)作為一組過濾
models.Classes.objects.filter(id=2) # select * from classes where id = 2
models.Classes.objects.filter(id__gt=2) # select * from classes where id > 2
models.Classes.objects.filter(id__gte=2) # select * from classes where id >= 2
models.Classes.objects.filter(id__lt=2) # select * from classes where id < 2
models.Classes.objects.filter(id__lte=2) # select * from classes where id <= 2
models.Classes.objects.exclude(id=2) # select * from classes where id != 2
models.Classes.objects.values().filter(id__lte=2,name='nick') # select * from classes where id <= 2 and name ='nick'
models.Classes.objects.filter(id__in=[2,3]) # select * from classes where id in (2,3)
models.Classes.objects.exclude(id__in=[2,3]) # select * from classes where id not in (2,3)
models.Classes.objects.filter(id__range=[1,3]) # select * from classe where id between 1 and 3
models.Classes.objects.filter(name__endswith='on') # select * from classes where name like "%on"
models.Classes.objects.filter(name__iendswith='on') # select * from classes where name like "%on" 匹配時忽略大小寫
models.Classes.objects.filter(name__startswith='aaa') # select * from classes where name like "aaa%"
models.Classes.objects.filter(name__istartswith='bbb') # select * from classes where name like "bbb%" 匹配時忽略大小寫
models.Classes.objects.filter(name__contains='ccc') # select * from classes where name like "%ccc%"
models.Classes.objects.filter(name__icontains='ccc') # select * from classes where name like "%ccc%" 匹配時忽略大小寫
models.Classes.objects.filter(name__regex='\w+') # 正則表達式匹配
models.Classes.objects.filter(name__iregex='\w+') # 正則表達式匹配,忽略大小寫
models.Classes.objects.count() # select count(*) from classes
models.Classes.objects.filter(id__gt=2).count() # select count(*) from classes where id > 2
models.Classes.objects.values().order_by('age') # select * from classes order by age 升序
models.Classes.objects.values().order_by('-age') # select * from classes order by age desc 降序
models.Classes.objects.values().order_by('age','-id') # select * from classes order by age asc,id desc 先按age升序排,age相同的按id降序排
from django.db.models import Count,Min,Max,Sum,Avg
res = models.Classes.objects.values('name').annotate(s=Sum('age')) # select name,sum(age) as s from classes group by name; 通過名字分組,在統計每組的年齡之和
res = models.Classes.objects.values('name').annotate(s=Sum('age')).filter(s__gt=30) # 相當於select name,sum(age) as s from classes group by name having(s>30);
models.Classes.objects.values()[1:3] # 通過切片獲取查詢數 select * from classes limit 1,2
models.Classes.objects.last() # 返回的是最後一個對象
models.Classes.objects.first() # 返回的是第一個對象
models.Classes.objects.only('name') # 只取name字段,但也會查id字段
models.Classes.objects.defer('name') # 不查name字段。select id, age from classes。指定為id時無效,還是會顯示id字段
from django.db.models import Q
models.Classes.objects.values().filter(Q(id__gte=3)|Q(name='nick')) # select * from classes where id>=3 or name='nick'
models.Classes.objects.values().filter((Q(id__gte=3)|Q(name='nick'))&Q(age=19)) # select * from classes where (id>=3 or name='nick') and age=19
from django.db.models import F
models.Classes.objects.update(age=F('age')+1) # 將每個人的年齡加1
以上就是常用的一些orm查詢,如果對你有所幫助,請點個贊,謝謝!