简体   繁体   English

如何按年度过滤sqlalchemy中的查询(日期时间列)

[英]How to filter query in sqlalchemy by year (datetime column)

I have table in sqlalchemy 0.4 that with types.DateTime column: 我在sqlalchemy 0.4中有使用types.DateTime列的表:

Column("dfield", types.DateTime, index=True)

I want to select records, that has specific year in this column, using model. 我想使用模型选择本专栏中具有特定年份的记录。 How to do this? 这该怎么做? I though it should be done like this: 我虽然应该像这样做:

selected_year = 2009
my_session = model.Session()
my_query = my_session.query(model.MyRecord).filter(model.dfield.??? == selected_year)

# process data in my_query

Part with ??? 部分与??? is for me unclear. 对我来说不清楚。

sqlalchemy.extract('year', model.MyRecord.dfield) == selected_year

对于referene: https ://docs.sqlalchemy.org/en/13/core/sqlelement.html#sqlalchemy.sql.expression.extract

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

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