Skip to content

Instantly share code, notes, and snippets.

@Tukki
Created October 25, 2012 16:52
Show Gist options
  • Save Tukki/3953990 to your computer and use it in GitHub Desktop.
Save Tukki/3953990 to your computer and use it in GitHub Desktop.
filter by time in sqlalchemy
#http://stackoverflow.com/questions/7075828/make-sqlalchemy-use-date-in-filter-using-postgresql
from sqlalchemy import Date, cast
from datetime import date
my_date = (session.query(MyObject)
.filter(cast(MyObject.date_time, Date) == date.today())
.all())
#or , just like sql
query = session.query(MyObject).filter(MyObject.create_time >= '2012-10-26')
query = (session.query(MyObject)
.filter(and_(MyObject.create_time >='2012-10-25', MyObject.create_time <= '2012-10-26')))
query = (session.query(MyObject)
.filter(MyObject.create_time.between('2012-10-25', '2012-10-26')))
@Tukki
Copy link
Author

Tukki commented Jun 16, 2013

使用case的方式, 将丢失索引的好处。

@ronelliott
Copy link

Thanks!

@LookOnTheBrightSide
Copy link

thanks!

@phuongdpham
Copy link

Thank you.

@mbdrian
Copy link

mbdrian commented Jan 11, 2018

it's work by filter(cast(MyObject.date_time, Date) == date.today())
thank youu

@chis
Copy link

chis commented Oct 1, 2018

Thank you for this, spent a while trying to find what I needed and this was it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment