Skip to content
Advertisement

Make Sqlalchemy Use Date In Filter Using Postgresql

I’m trying to perform the following query in Sqlalchemy.

Select * from "Mytable" where Date(date_time_field) = "2011-08-16";

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.

Is it possible at the ORM level to declare a Simple Date() around the queried field?

Advertisement

Answer

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()
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement