使用PostgreSQL使Sqlalchemy在过滤器中使用日期 [英] Make Sqlalchemy Use Date In Filter Using Postgresql
问题描述
我正在尝试在Sqlalchemy中执行以下查询。
I'm trying to perform the following query in Sqlalchemy.
Select * from "Mytable" where Date(date_time_field) = "2011-08-16";
我尝试了几种方法。一些在这里。
但是,似乎没有一个是现实的,因为某些类型会强制转换其他字符串格式?而不是简单的Date()(按日期,我的意思是Postgresql日期而不是Python的日期),位于ORM字段。
I have tried several methods. Some here on SO. But none seems "realistic" since some do Casts Other String Formating ?!?! and not a plain simple Date() ( By Date i Mean the Postgresql Date not the Python One) at the ORM Field.
是吗在ORM级别可以在查询的字段周围声明一个简单的Date()吗?
Is it possible at the ORM level to declare a Simple Date() around the queried field ?
最好的问候
António
Best Regards António
推荐答案
使用@Ants Aasma注释。
Using @Ants Aasma Comment.
并在进行任何网络搜索时保持其清洁。
And to Keep it clean for any web search.
from sqlalchemy import Date, cast
from datetime import date
my_data = session.query(MyObject).\
filter(cast(MyObject.date_time,Date) == date.today()).all()
谢谢所有尝试解决此问题的人:)
Thank you all who tried to solve this problem :)
这篇关于使用PostgreSQL使Sqlalchemy在过滤器中使用日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!