繁体   English   中英

SQLAlchemy:在 JSON 字段中按日期时间过滤

[英]SQLAlchemy: filter by datetime in JSON field

我需要根据 JSON DB 字段(postgres)中的日期过滤查询。

例如。 对于 integer 比较,此语法有效:

r = Customer.query.filter(Customer.jsondata[('basic', 'age')].astext.cast(Integer) > 18).all()

但是这个查询对于不超过 12 个月的 iso 格式的日期时间是如何工作的:

r = Customer.query.filter(Customer.jsondata[('basic', 'dateCreated')].astext.cast(???) > comparison_value).all()

我需要投什么?

比较值可以是什么类型?

我的想法是正确的概念吗?

任何帮助表示赞赏!

感谢 ThiefMaster 的评论,解决方案非常简单。

我用了:

from sqlalchemy.types import DateTime
import pendulum

r = Customer.query.filter(
      Customer.custcontext_json[('basic', 'dateCreated')].astext.cast(DateTime) > pendulum.now('UTC').subtract(months= 12)
      ).all()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM