Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
721 views
in Technique[技术] by (71.8m points)

python - How to filter query in sqlalchemy by year (datetime column)

I have table in sqlalchemy 0.4 that with types.DateTime column:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
sqlalchemy.extract('year', model.MyRecord.dfield) == selected_year

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...