Home > Software design >  How to filter SQLAlchemy objects with datefield values before today?
How to filter SQLAlchemy objects with datefield values before today?

Time:10-15

I wish to keep all objects from the database which have a particular datetime field with values before the current day. I can see how you an filter with hardcoded dates or between two dates, but how can I keep all items with dates before today?

CodePudding user response:

So it seems it is quite straight forward you can do direct datetime comparison in SQLAlchemy queries like this:

    q = DBSession.query(User).filter(
        User.sign_up_date <= datetime.now() - datetime.timedelta(hours=1),
    )

which would return all user objects which signed up one hour ago.

  • Related