ITeye博客

flask-sqlachemy note

essen · 2012-12-10 17:15 · python · 编程语言 · Markdown

between

datetime按天查询

 

Myclass.query.filter(myClass.date.between(_date, _date+timedelta(days = 1)))

 

filter 和 filter_by的区别

 

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

 

filter 需要带上tablename,并用==

http://docs.sqlalchemy.org/en/rel_0_7/orm/query.html?highlight=filter_by#sqlalchemy.orm.query.Query.filter

 

 

filter 多个条件的查询

AND操作

刚开始想用惯常的sql查询思维

tablename.filter(condition1 and condition2).all()

 

生成数据后发现condition2完全没有执行。

网上找了个折中的办法:

tablename.fiter(conditon1).filter(condition2).all()

 

虽然达到了想要的效果,但是有种隔靴搔痒的感觉。查找sqlachemy的代码,发现对于查询条件,是放在一个list中进行处理的。各个查询条件之间用‘,’分割:

tablename.filter(condition1,condition2).all

 

OR操作

from sqlachemy import or_
myclass.query.filter(or_(myclass.name=='xk', myclass.country=='china'))

评论(1)

  1. #1 楼 · heipark · 2012-12-10 22:25

    Good!

评论功能维护中,当前仅展示历史评论。