SQLAlchemy中filter()和filter_by()有什么区别
from:https://segmentfault.com/q/1010000000140472
apply the given filtering criterion to a copy of this Query, using SQL expressions.
e.g.:
session.query(MyClass).filter(MyClass.name == 'some name')
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)sand后面的条件既不报错,又不生效,很坑。
要实现组合查询,要么连续调用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()有什么区别的更多相关文章
- SQLAlchemy中filter()和filter_by()的区别
1.filter引用列名时,使用“类名.属性名”的方式,比较使用两个等号“==” 2.filter_by引用列名时,使用“属性名”,比较使用一个等号“=” 3.在使用多条件匹配的时候,filter需要 ...
- SQLAlchemy中filter和filer_by的区别
filter: session.query(MyClass).filter(MyClass.name == 'some name') filter_by: session.query(MyClass) ...
- flask中filter和filter_by的区别
filter_by表内部精确查询 User.query.filter_by(id=4).first() filter 全局查询 id必须指明来源于那张表User,而且需要用等号,而不是赋值 User. ...
- flask sqlaichemy中filter和filter_by
简单总结一下: 查询的三种方式: 要实现组合查询,要么连续调用filter:q = sess.query(IS).filter(IS.node == node).filter(IS.password ...
- python中filter、map、reduce的区别
python中有一些非常有趣的函数,今天也来总结一下,不过该类的网上资料也相当多,也没多少干货,只是习惯性将一些容易遗忘的功能进行整理. lambda 为关键字.filter,map,reduce为内 ...
- flask SQLAlchemy中一对多的关系实现
SQLAlchemy是Python中比较优秀的orm框架,在SQLAlchemy中定义了多种数据库表的对应关系, 其中一对多是一种比较常见的关系.利用flask sqlalchemy实现一对多的关系如 ...
- 简单理解Struts2中拦截器与过滤器的区别及执行顺序
简单理解Struts2中拦截器与过滤器的区别及执行顺序 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标 ...
- CSS3 filter:drop-shadow滤镜与box-shadow区别应用 抄的
CSS3 filter:drop-shadow滤镜与box-shadow区别应用 这篇文章发布于 2016年05月18日,星期三,01:07,归类于 css相关. 阅读 5777 次, 今日 12 次 ...
- JDBC中的Statement和PreparedStatement的区别
JDBC中的Statement和PreparedStatement的区别
随机推荐
- 问题 B: Prime Number
题目描述 Output the k-th prime number. 输入 k≤10000 输出 The k-th prime number. 样例输入 10 50 样例输出 29 229 #incl ...
- LINQ学习笔记——(3)基本查询操作符
Select() 作用于uIEnumerable<TSource>类型 public static void Test() { List<string> persons = n ...
- python基础训练营04-函数
任务四 函数的关键字 函数的定义 函数参数与作用域 函数返回值 一.函数的关键字: def 二.函数的定义: 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号 ...
- LoadRunner系统架构简介与运行原理
1.LoadRunner系统架构简介 LoadRunner是通过创建虚拟用户来代替真实实际用户来操作客户端软件比如Internet Explorer,来向IIS.Apache等Web服务器发送HTTP ...
- PHP与webserver【简书看到的】
很久以前,人们造出来一个机器人,它的英文名字叫web server,中文名叫网页服务器.(为了简写,下文称web server为server) server的工作很简单,就是做内容的分发. 初期的se ...
- c++ 中反正单词用到了resize()
resize(n) 调整容器的长度大小,使其能容纳n个元素.如果n小于容器的当前的size,则删除多出来的元素.否则,添加采用值初始化的元素. 题目如下: 151. Reverse Words in ...
- AGC016B Colorful Hats(构造)
题目大意: 给定n和n个数,每个数a[i]代表除了i外序列中颜色不同的数的个数,问能否构造出来这个数列. 比较简单,首先先求出来a数列的最大值Max, 如果有数小于Max-1,那么显然是不存在的 接下 ...
- 【题解】洛谷P2418 yyy loves OI IV
感觉很是妙啊……这题数次误入歧途...最开始想的二维dp,单调队列优化:无果,卒.于是没忍住看了下标签:暴力枚举?搜索?于是开始想记忆化搜索.以为会有什么很强的剪枝之类的:30分,卒.最后终于回到正道 ...
- [Leetcode] Reorder list 重排链表
Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You ...
- BZOJ2729 [HNOI2012]排队 【高精 + 组合数学】
题目链接 BZOJ2729 题解 高考数学题... 我们先把老师看做男生,女生插空站 如果两个老师相邻,我们把他们看做一个男生,女生插空站 对于\(n\)个男生\(m\)个女生的方案数: \[n!m! ...