I have a simple SQLAlchemy model:
class Event(Base):
""" The SQLAlchemy declarative model class for a Event object. """
__tablename__ = 'events'
id = Column(Integer, primary_key=True)
date = Column(DateTime)
title = Column(Text)
How do I filter a SQLAlchemy query so that only events in a specific month are returned?
A very old question but a better answer is here:
from sqlalchemy import extract
session.query(Event).filter(extract('month', Event.date)==7).all()
This returns all the records into a database events in July.
This should find all the events in June, 2012.
import datetime
import calendar
year = 2012
month = 6
num_days = calendar.monthrange(year, month)[1]
start_date = datetime.date(year, month, 1)
end_date = datetime.date(year, month, num_days)
results = session.query(Event).filter(
and_(Event.date >= start_date, Event.date <= end_date)).all()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With