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. JavaScript - arguments object

    The arguments object is an Array-like object corresponding to the arguments passed to a function. fu ...

  2. HDU 1445 Ride to School

    http://acm.hdu.edu.cn/showproblem.php?pid=1445 Problem Description Many graduate students of Peking ...

  3. Java的同步容器和并发容器

    前言: 之前在介绍Java集合的时候说到,java提供的实现类很少是线程安全的.只有几个比较古老的类,比如Vector.Hashtable等是线程安全的,尤其是Hashtable,古老到连命名规范都没 ...

  4. [剑指Offer] 24.二叉树中和为某一值的路径

    [思路] ·递归先序遍历树, 把结点加入路径. ·若该结点是叶子结点则比较当前路径和是否等于期待和. ·弹出结点,每一轮递归返回到父结点时,当前路径也应该回退一个结点 注:路径定义为从树的根结点开始往 ...

  5. 【bzoj5060】魔方国 乱搞+特判

    题目描述 一张未知的有重边无自环的图,只知道点数为n,边数为m.可以标记若干个点,如果一个点被标记,那么与它距离不超过k的点(包括本身)都会被覆盖. 显然对于每张不同图,让所有点被覆盖的最小代价是不一 ...

  6. BZOJ3073 PA2011Journeys(线段树+bfs)

    线段树优化建图裸题.建两棵线段树,一棵表示入一棵表示出.对题中所给的边新建一个虚拟点,将两段区间拆成线段树上对应区间,出线段树中对应区间所表示的点向虚拟点连边权0的边,虚拟点向入线段树中对应区间所表示 ...

  7. DataBase -- Second Highest Salary

    Question: Write a SQL query to get the second highest salary from the Employee table. +----+-------- ...

  8. BZOJ3211 花神游历各国 【树状数组 + 并查集】

    题目 输入格式 输出格式 每次x=1时,每行一个整数,表示这次旅行的开心度 输入样例 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 2 3 1 1 4 输出样例 101 11 1 ...

  9. CF840C On the Bench 解题报告

    CF840C On the Bench 题意翻译 给定\(n\) \((1≤n≤300)\) 个数,求问有多少种排列方案使得任意两个相邻的数之积都不是完全平方数.由于方案数可能很大,输出方案数 \(m ...

  10. BZOJ 2707: [SDOI2012]走迷宫 拓扑+高斯消元+期望概率dp+Tarjan

    先Tarjan缩点 强连通分量里用高斯消元外面直接转移 注意删掉终点出边和拓扑 #include<cstdio> #include<cstring> #include<a ...