我正在尝试将datetime
列转换为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
错误显示:"name 'DATE' is not defined"
我试着加入import DATE
,得到"ImportError:cannot import name 'DATE'"
也许我从一开始就做错了。请指导。
你必须导入它。
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
或者你可以用func:
from sqlalchemy import func
...
query = query.filter(func.date(db_col) == date_from)