zoukankan      html  css  js  c++  java
  • python测试开发django171.ORM查询之exact和iexact 上海

    前言

    平常用ORM大部分使用的是get、filter、exclude这三种能满足基本的需求。
    ORM 条件查询使用field__结合 condition 的方式来使用的,本篇讲解下exact和iexact 在使用上有什么区别。

    exact 精准查询

    exact使用精确的 = 查找,如果传None参数,在SQL 中会被解释为 NULL

    >>> Product.objects.filter(name__exact='yy')
    <QuerySet [<Product: Product object (2)>, <Product: Product object (5)>]>
    >>> Product.objects.filter(name__exact=None)
    <QuerySet []>
    

    上面两个查询可以等价于以下SQL

    select * from yoyo_product where name='yy'; 
    select * from yoyo_product where name is NULL; 
    

    filter(name__exact='yy') 实际上是等价于 filter(name='yy')

    iexact 使用 like 查找

    iexact 使用 like 查找,如

    >>> Product.objects.filter(name__iexact='yy')
    <QuerySet [<Product: Product object (2)>, <Product: Product object (5)>]>
    

    上面查询可以等价于以下SQL

    select * from yoyo_product where name like 'yy'; 
    

    exact 和 iexact区别

    exact 和 iexact 的区别实际上就是 = 和 LIKE 的区别
    这两个参数会受到你的SQL的所在的安装系统有关系。如果你的是Window系统,那么就会不区分大小写,相反Linux下是区分大小写的。
    这两个参数还受你的数据库的排序规则的这个参数影响。在大部分collation=utf8_general_ci 情况下都是一样的(collation 是用来对字符串比较的)
    实际开发中使用 exact 和 iexact 很少,直接使用:field=xx 即可

  • 相关阅读:
    端口扫描技术
    HBase——常用命令
    RabbitMQ——常用命令
    RabbitMQ——基于 KeepAlived + HAProxy 搭建 RabbitMQ 高可用负载均衡集群
    RabbitMQ——安装、集群搭建、镜像队列配置
    Zookeeper——常用命令
    MFC子对话框嵌入主对话框
    Find a way
    Avoid The Lakes
    变形课
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/15528860.html
Copyright © 2011-2022 走看看