将字符串转换为datetime并与SQLAlchemy中的日期进行比较

时间:2018-04-16 08:39:26

标签: python sqlalchemy

我的模型中有一个DateTime列,目前我正在尝试过滤大于字符串“2018-04-11 04:03:20”所代表的日期的条目:

class SomeType(object):
    created_at = Column(DateTime, default=lambda: datetime.utcnow())

beginning_time = datetime.strptime("2018-04-11 04:03:20", '%Y-%m-%d %H:%M:%S')

candidates = session.query(SomeType).filter(SomeType.created_at >= beginning_time).all()

问题是candidates总是包含一些不符合标准的记录。错误结果的样本:

{
    "inspection_name": "lsof check",
    "name": "lsof check",
    "executor": "Susan",
    "created_at": "2018-04-17T11:12:08.000000",
    "score": 43,
    "inspection_groups": "CLM EC",
    "id": "125d932e-293e-469f-8cd2-d08f44bf2ab8",
    "updated_at": "2018-04-17T11:12:23.000000"
},
{
    "inspection_name": "lsof-fake",
    "name": "lsof-fake",
    "executor": "Susan",
    "created_at": "2018-03-21T14:34:19.000000",
    "score": 40,
    "inspection_groups": "CLM EC",
    "id": "1278309f-7dd6-49f4-9d3d-f44097b4baf8",
    "updated_at": "2018-03-21T14:34:21.000000"
},

但是当我使用由datetime生成的日期时:

beginning_time = datetime.utcnow()-timedelta(days=2)

我可以得到完全正确的候选人。关于如何以正确的方式做到这一点的任何想法?

0 个答案:

没有答案