《Django model update的各种用法介绍》文章介绍了Django model的各种update操作,这篇文章就是她的姊妹篇,详细介绍Django model select的用法,配以对应MySQL的查询语句,理解起来更轻松。

基本操作

# 获取所有数据,对应SQL:select * from User
User.objects.all() # 匹配,对应SQL:select * from User where name = '运维咖啡吧'
User.objects.filter(name='运维咖啡吧') # 不匹配,对应SQL:select * from User where name != '运维咖啡吧'
User.objects.exclude(name='运维咖啡吧') # 获取单条数据(有且仅有一条,id唯一),对应SQL:select * from User where id = 724
User.objects.get(id=123)

常用操作

# 获取总数,对应SQL:select count(1) from User
User.objects.count() # 获取总数,对应SQL:select count(1) from User where name = '运维咖啡吧'
User.objects.filter(name='运维咖啡吧').count() # 大于,>,对应SQL:select * from User where id > 724
User.objects.filter(id__gt=724) # 大于等于,>=,对应SQL:select * from User where id >= 724
User.objects.filter(id__gte=724) # 小于,<,对应SQL:select * from User where id < 724
User.objects.filter(id__lt=724) # 小于等于,<=,对应SQL:select * from User where id <= 724
User.objects.filter(id__lte=724) # 同时大于和小于, 1 < id < 10,对应SQL:select * from User where id > 1 and id < 10
User.objects.filter(id__gt=1, id__lt=10) # 包含,in,对应SQL:select * from User where id in (11,22,33)
User.objects.filter(id__in=[11, 22, 33]) # 不包含,not in,对应SQL:select * from User where id not in (11,22,33)
User.objects.exclude(id__in=[11, 22, 33]) # 为空:isnull=True,对应SQL:select * from User where pub_date is null
User.objects.filter(pub_date__isnull=True) # 不为空:isnull=False,对应SQL:select * from User where pub_date is not null
User.objects.filter(pub_date__isnull=False) # 匹配,like,大小写敏感,对应SQL:select * from User where name like '%sre%',SQL中大小写不敏感
User.objects.filter(name__contains="sre") # 匹配,like,大小写不敏感,对应SQL:select * from User where name like '%sre%',SQL中大小写不敏感
User.objects.filter(name__icontains="sre") # 不匹配,大小写敏感,对应SQL:select * from User where name not like '%sre%',SQL中大小写不敏感
User.objects.exclude(name__contains="sre") # 不匹配,大小写不敏感,对应SQL:select * from User where name not like '%sre%',SQL中大小写不敏感
User.objects.exclude(name__icontains="sre") # 范围,between and,对应SQL:select * from User where id between 3 and 8
User.objects.filter(id__range=[3, 8]) # 以什么开头,大小写敏感,对应SQL:select * from User where name like 'sh%',SQL中大小写不敏感
User.objects.filter(name__startswith='sre') # 以什么开头,大小写不敏感,对应SQL:select * from User where name like 'sh%',SQL中大小写不敏感
User.objects.filter(name__istartswith='sre') # 以什么结尾,大小写敏感,对应SQL:select * from User where name like '%sre',SQL中大小写不敏感
User.objects.filter(name__endswith='sre') # 以什么结尾,大小写不敏感,对应SQL:select * from User where name like '%sre',SQL中大小写不敏感
User.objects.filter(name__iendswith='sre') # 排序,order by,正序,对应SQL:select * from User where name = '运维咖啡吧' order by id
User.objects.filter(name='运维咖啡吧').order_by('id') # 多级排序,order by,先按name进行正序排列,如果name一致则再按照id倒叙排列
User.objects.filter(name='运维咖啡吧').order_by('name','-id') # 排序,order by,倒序,对应SQL:select * from User where name = '运维咖啡吧' order by id desc
User.objects.filter(name='运维咖啡吧').order_by('-id')

进阶操作

# limit,对应SQL:select * from User limit 3;
User.objects.all()[:3] # limit,取第三条以后的数据,没有对应的SQL,类似的如:select * from User limit 3,10000000,从第3条开始取数据,取10000000条(10000000大于表中数据条数)
User.objects.all()[3:] # offset,取出结果的第10-20条数据(不包含10,包含20),也没有对应SQL,参考上边的SQL写法
User.objects.all()[10:20] # 分组,group by,对应SQL:select username,count(1) from User group by username;
from django.db.models import Count
User.objects.values_list('username').annotate(Count('id')) # 去重distinct,对应SQL:select distinct(username) from User
User.objects.values('username').distinct().count() # filter多列、查询多列,对应SQL:select username,fullname from accounts_user
User.objects.values_list('username', 'fullname') # filter单列、查询单列,正常values_list给出的结果是个列表,里边里边的每条数据对应一个元组,当只查询一列时,可以使用flat标签去掉元组,将每条数据的结果以字符串的形式存储在列表中,从而避免解析元组的麻烦
User.objects.values_list('username', flat=True) # int字段取最大值、最小值、综合、平均数
from django.db.models import Sum,Count,Max,Min,Avg User.objects.aggregate(Count(‘id’))
User.objects.aggregate(Sum(‘age’))

时间字段

# 匹配日期,date
User.objects.filter(create_time__date=datetime.date(2018, 8, 1))
User.objects.filter(create_time__date__gt=datetime.date(2018, 8, 2)) # 匹配年,year
User.objects.filter(create_time__year=2018)
User.objects.filter(create_time__year__gte=2018) # 匹配月,month
User.objects.filter(create_time__month__gt=7)
User.objects.filter(create_time__month__gte=7) # 匹配日,day
User.objects.filter(create_time__day=8)
User.objects.filter(create_time__day__gte=8) # 匹配周,week_day
User.objects.filter(create_time__week_day=2)
User.objects.filter(create_time__week_day__gte=2) # 匹配时,hour
User.objects.filter(create_time__hour=9)
User.objects.filter(create_time__hour__gte=9) # 匹配分,minute
User.objects.filter(create_time__minute=15)
User.objects.filter(create_time__minute_gt=15) # 匹配秒,second
User.objects.filter(create_time__second=15)
User.objects.filter(create_time__second__gte=15) # 按天统计归档
today = datetime.date.today()
select = {'day': connection.ops.date_trunc_sql('day', 'create_time')}
deploy_date_count = Task.objects.filter(
create_time__range=(today - datetime.timedelta(days=7), today)
).extra(select=select).values('day').annotate(number=Count('id'))

Q 的使用

Q对象可以对关键字参数进行封装,从而更好的应用多个查询,可以组合&(and)、|(or)、~(not)操作符。

例如下边的语句

from django.db.models import Q

User.objects.filter(
Q(role__startswith='sre_'),
Q(name='公众号') | Q(name='运维咖啡吧')
)

转换成SQL语句如下:

select * from User where role like 'sre_%' and (name='公众号' or name='运维咖啡吧')

通常更多的时候我们用Q来做搜索逻辑,比如前台搜索框输入一个字符,后台去数据库中检索标题或内容中是否包含

_s = request.GET.get('search')

_t = Blog.objects.all()
if _s:
_t = _t.filter(
Q(title__icontains=_s) |
Q(content__icontains=_s)
) return _t

外键:ForeignKey

表结构:

class Role(models.Model):
name = models.CharField(max_length=16, unique=True) class User(models.Model):
username = models.EmailField(max_length=255, unique=True)
role = models.ForeignKey(Role, on_delete=models.CASCADE)

正向查询:

# 查询用户的角色名
_t = User.objects.get(username='运维咖啡吧')
_t.role.name

反向查询:

# 查询角色下包含的所有用户
_t = Role.objects.get(name='Role03')
_t.user_set.all()

另一种反向查询的方法:

_t = Role.objects.get(name='Role03')

# 这种方法比上一种_set的方法查询速度要快
User.objects.filter(role=_t)

第三种反向查询的方法:

如果外键字段有related_name属性,例如models如下:

class User(models.Model):
username = models.EmailField(max_length=255, unique=True)
role = models.ForeignKey(Role, on_delete=models.CASCADE,related_name='roleUsers')

那么可以直接用related_name属性取到某角色的所有用户

_t = Role.objects.get(name = 'Role03')
_t.roleUsers.all()

M2M:ManyToManyField

表结构:

class Group(models.Model):
name = models.CharField(max_length=16, unique=True) class User(models.Model):
username = models.CharField(max_length=255, unique=True)
groups = models.ManyToManyField(Group, related_name='groupUsers')

正向查询:

# 查询用户隶属组
_t = User.objects.get(username = '运维咖啡吧')
_t.groups.all()

反向查询:

# 查询组包含用户
_t = Group.objects.get(name = 'groupC')
_t.user_set.all()

同样M2M字段如果有related_name属性,那么可以直接用下边的方式反查

_t = Group.objects.get(name = 'groupC')
_t.groupUsers.all()

get_object_or_404

正常如果我们要去数据库里搜索某一条数据时,通常使用下边的方法:

_t = User.objects.get(id=734)

但当id=724的数据不存在时,程序将会抛出一个错误

abcer.models.DoesNotExist: User matching query does not exist.

为了程序兼容和异常判断,我们可以使用下边两种方式:

方式一:get改为filter

_t = User.objects.filter(id=724)
# 取出_t之后再去判断_t是否存在

方式二:使用get_object_or_404

from django.shortcuts import get_object_or_404

_t = get_object_or_404(User, id=724)
# get_object_or_404方法,它会先调用django的get方法,如果查询的对象不存在的话,则抛出一个Http404的异常

实现方法类似于下边这样:

from django.http import Http404

try:
_t = User.objects.get(id=724)
except User.DoesNotExist:
raise Http404

get_or_create

顾名思义,查找一个对象如果不存在则创建,如下:

object, created = User.objects.get_or_create(username='运维咖啡吧')

返回一个由object和created组成的元组,其中object就是一个查询到的或者是被创建的对象,created是一个表示是否创建了新对象的布尔值

实现方式类似于下边这样:

try:
object = User.objects.get(username='运维咖啡吧') created = False
exception User.DoesNoExist:
object = User(username='运维咖啡吧')
object.save() created = True returen object, created

执行原生SQL

Django中能用ORM的就用它ORM吧,不建议执行原生SQL,可能会有一些安全问题,如果实在是SQL太复杂ORM实现不了,那就看看下边执行原生SQL的方法,跟直接使用pymysql基本一致了

from django.db import connection

with connection.cursor() as cursor:
cursor.execute('select * from accounts_User')
row = cursor.fetchall() return row

注意这里表名字要用app名+下划线+model名的方式

本文内容来自群友的公众号:运维咖啡吧 ,欢迎大家关注,原文地址:https://mp.weixin.qq.com/s/JVh4UnS2Tql9gUVaBSoGuA

Django model select的各种用法详解的更多相关文章

  1. AngularJS select中ngOptions用法详解

    AngularJS select中ngOptions用法详解   一.用法 ngOption针对不同类型的数据源有不同的用法,主要体现在数组和对象上. 数组: label for value in a ...

  2. Django model 中的 class Meta 详解

    Django model 中的 class Meta 详解 通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(mode ...

  3. Django model中的class Meta详解

    通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(models.Model): bar = models.CharFi ...

  4. Django model中的 class Meta 详解

    通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(models.Model): bar = models.CharFi ...

  5. Django框架 之 ORM查询操作详解

    Django框架 之 ORM查询操作详解 浏览目录 一般操作 ForeignKey操作 ManyToManyField 聚合查询 分组查询 F查询和Q查询 事务 Django终端打印SQL语句 在Py ...

  6. Vue1.0用法详解

    Vue.js 不支持 IE8 及其以下版本,因为 Vue.js 使用了 IE8 不能实现的 ECMAScript 5 特性. 开发环境部署 可参考使用 vue+webpack. 基本用法 1 2 3 ...

  7. @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...

  8. jQuery 事件用法详解

    jQuery 事件用法详解 目录 简介 实现原理 事件操作 绑定事件 解除事件 触发事件 事件委托 事件操作进阶 阻止默认事件 阻止事件传播 阻止事件向后执行 命名空间 自定义事件 事件队列 jque ...

  9. SQL中CONVERT()函数用法详解

    SQL中CONVERT函数格式: CONVERT(data_type,expression[,style]) 参数说明: expression 是任何有效的 Microsoft® SQL Server ...

随机推荐

  1. 为什么要使用mybaits

    通常在项目开发过程中,有很多代码是重复的,固定不变的.为了提升开发效率,可将这些 固定不变的代码提取出来,生成class文件,将class文件打jar包,基于框架开发. mybaits是一个ORM框架 ...

  2. Curator场景应用

    分布式锁功能: 在分布式场景中,我们为了保证数据的一致性,经常在程序运行的某一个点,需要进行同步操作,(java提供synchronized或者Reentrantlock实现), 使用curator基 ...

  3. Java高级

    1.GC是什么?为什么要有GC? GC是垃圾收集的意思(Gabage Collection),内存处理是编程人员容易出现问题的地方,忘记或者错误的内存回收会导致程序或系统的不稳定甚至崩溃,Java提供 ...

  4. CentOS 7升级Python到3.5后yum出错

    CentOS 7升级Python到3.5后,我跟以前CentOS 6一样,在/usr/bin/python创建了一个指向Python 3的软连接,然后将/usr/bin/yum的顶部的: !/usr/ ...

  5. Android R文件介绍

    R.java 文件中默认有attr.drawable.layout.string等色哥静态内部类,每个静态内部类分别对应着一种资源,如layout静态内部类对应着layout中的接界面文件,其中每个静 ...

  6. windows下webstorm调试react native

    安装请参考:http://www.cnblogs.com/wjx0912/p/5662457.html webstorm在2016.10.20的版本才正式增加react native的调试支持,所以w ...

  7. 201621123002《Java程序设计》第七周学习总结

    1. 本周学习总结 1.1 思维导图:Java图形界面总结 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 事件源,事件处理方法,事件监听器 事件源(Event ...

  8. AX_SysExcel

    void KTL_CPeng_ImportCustStamp()  {      str                 file;      FileNameFilter      filter = ...

  9. 1.3eigen中数组类和系数的运算

    1.3数组类和系数的运算 与矩阵类只适用与线性代数运算相反,数组类提供通用的数组类,能不利用线性代数的知识来对系数进行操作,比如对每个系数加上一个常数,或者乘上两个数组的系数. 1.数组类型 跟矩阵类 ...

  10. js中加“var”和不加“var”的区别

    JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: var x // x 为 undefined var x = 6; // x 为数字 var x = "Bill&q ...