from:https://segmentfault.com/q/1010000000140472

filter:

apply the given filtering criterion to a copy of this Query, using SQL expressions.
e.g.:
session.query(MyClass).filter(MyClass.name == 'some name')

filter_by:

apply the given filtering criterion to a copy of this Query, using keyword expressions.
e.g.:
session.query(MyClass).filter_by(name = 'some name')

确实,filter用类名.属性名,比较用==,filter_by直接用属性名,比较用=
不过这个是语法小细节。

个人觉得最重要的区别是filter不支持组合查询,只能连续调用filter来变相实现。
而filter_by的参数是**kwargs,直接支持组合查询。
比如:

q = sess.query(IS).filter(IS.node == node and IS.password == password).all()
对应的sql是

SELECT tb_is.id AS tb_is_id, tb_is.node AS tb_is_node, tb_is.password AS tb_is_password, tb_is.email AS tb_is_email, tb_is.`admin` AS tb_is_admin, tb_is.contact AS tb_is_contact, tb_is.is_available AS tb_is_is_available, tb_is.is_url AS tb_is_is_url, tb_is.note AS tb_is_note
FROM tb_is
WHERE tb_is.node = %(node_1)s

and后面的条件既不报错,又不生效,很坑。

要实现组合查询,要么连续调用filter:
q = sess.query(IS).filter(IS.node == node).filter(IS.password == password).all()

或者直接用filter_by:
q = sess.query(IS).filter_by(node=node, password=password).all()

两者都对应sql:

 
SELECT tb_is.id AS tb_is_id, tb_is.node AS tb_is_node, tb_is.password AS tb_is_password, tb_is.email AS tb_is_email, tb_is.`admin` AS tb_is_admin, tb_is.contact AS tb_is_contact, tb_is.is_available AS tb_is_is_available, tb_is.is_url AS tb_is_is_url, tb_is.note AS tb_is_note
FROM tb_is
WHERE tb_is.password = %(password_1)s AND tb_is.node = %(node_1)s

SQLAlchemy中filter()和filter_by()有什么区别的更多相关文章

  1. SQLAlchemy中filter()和filter_by()的区别

    1.filter引用列名时,使用“类名.属性名”的方式,比较使用两个等号“==” 2.filter_by引用列名时,使用“属性名”,比较使用一个等号“=” 3.在使用多条件匹配的时候,filter需要 ...

  2. SQLAlchemy中filter和filer_by的区别

    filter: session.query(MyClass).filter(MyClass.name == 'some name') filter_by: session.query(MyClass) ...

  3. flask中filter和filter_by的区别

    filter_by表内部精确查询 User.query.filter_by(id=4).first() filter 全局查询 id必须指明来源于那张表User,而且需要用等号,而不是赋值 User. ...

  4. flask sqlaichemy中filter和filter_by

    简单总结一下: 查询的三种方式: 要实现组合查询,要么连续调用filter:q = sess.query(IS).filter(IS.node == node).filter(IS.password ...

  5. python中filter、map、reduce的区别

    python中有一些非常有趣的函数,今天也来总结一下,不过该类的网上资料也相当多,也没多少干货,只是习惯性将一些容易遗忘的功能进行整理. lambda 为关键字.filter,map,reduce为内 ...

  6. flask SQLAlchemy中一对多的关系实现

    SQLAlchemy是Python中比较优秀的orm框架,在SQLAlchemy中定义了多种数据库表的对应关系, 其中一对多是一种比较常见的关系.利用flask sqlalchemy实现一对多的关系如 ...

  7. 简单理解Struts2中拦截器与过滤器的区别及执行顺序

    简单理解Struts2中拦截器与过滤器的区别及执行顺序 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标 ...

  8. CSS3 filter:drop-shadow滤镜与box-shadow区别应用 抄的

    CSS3 filter:drop-shadow滤镜与box-shadow区别应用 这篇文章发布于 2016年05月18日,星期三,01:07,归类于 css相关. 阅读 5777 次, 今日 12 次 ...

  9. JDBC中的Statement和PreparedStatement的区别

    JDBC中的Statement和PreparedStatement的区别  

随机推荐

  1. 21天学习caffe(二)

    本文大致记录使用caffe的一次完整流程 Process 1 下载mnist数据集(数据量很小),解压放在data/mnist文件夹中:2 运行create_mnist.sh,生成lmdb格式的数据( ...

  2. python基础训练营02

    任务二 时长:2天 1. 列表 a. 标志 b. 基本操作(创建,append( ),pop( ) ,del( ), 拷贝) c. 列表相关方法 2. 元组 a. 标志 b. 基本操作(创建及不可变性 ...

  3. Spring Cloud文档地址大全

    Spring Cloud:http://cloud.spring.io/spring-cloud-static/spring-cloud.html Spring Cloud Config:http:/ ...

  4. Go基础篇【第8篇】: 内置库模块 bytes [二]

    type Reader ¶ type Reader struct { // 内含隐藏或非导出字段 } Reader类型通过从一个[]byte读取数据,实现了io.Reader.io.Seeker.io ...

  5. HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)

    Problem Description A password locker with N digits, each digit can be rotated to 0-9 circularly.You ...

  6. JavaSE复习(四)File类与IO流

    File类 构造方法 public File(String pathname) :通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例. public File(String parent ...

  7. Linux TCP协议使用的变量

    Linux /proc/sys/net/ipv4/* 变量 TCP变量:somaxconn - INTEGER    listen()的backlog参数的上限,在用户态为SOMAXCONN.默认是1 ...

  8. Struts2值栈

    一.前言 很多事儿啊,就是“成也萧何败也萧何”,细想一些事儿心中有感,当然,感慨和本文毛关系都没有~想起之前有篇Struts2中值栈的博客还未完工,就着心中的波澜,狂咽一把~ 二.正文 博文基于:st ...

  9. ASP.NET页面之间传值Application(5)

    Application对象的作用范围是整个全局,也就是说对所有用户都有效.它在整个应用程序生命周期中都是有效的,类似于使用全局变量一样,所 以可以在不同页面中对它进行存取.它和Session变量的区别 ...

  10. P1118 [USACO06FEB]数字三角形`Backward Digit Su`…

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 \le N \ ...