在使用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查询,如果对你有所帮助,请点个赞,谢谢!