deojeff
deojeff

Reputation: 377

SqlAlchemy: Convert datetime column to date column

I'm trying to convert the datetime column to date

def filter_between_dates(query, req, db_col):

    date_from = req.get_param('date_from')
    #date_to = req.get_param('date_to')

    query = query.filter(cast(db_col,DATE) == date_from)

    return query

The error showed: "name 'DATE' is not defined" I tried adding import DATE, and got "ImportError:cannot import name 'DATE'"

Maybe I'm doing this wrong from the beginning. Please guide.

Upvotes: 5

Views: 9202

Answers (1)

Raphael Amoedo
Raphael Amoedo

Reputation: 4455

You have to import it.

from sqlalchemy import Date, cast
...
def filter_between_dates(query, req, db_col):

    date_from = req.get_param('date_from')
    #date_to = req.get_param('date_to')

    query = query.filter(cast(db_col,Date) == date_from)

    return query

Or you can do it with func:

from sqlalchemy import func
...
query = query.filter(func.date(db_col) == date_from)

Upvotes: 8

Related Questions