简体   繁体   中英

How to filter a datetime field in flask-sqlalchemy

So I am trying to filter a field then delete all of those inside that field, but it just doesn't work. I am using flask-sqlalchemy not sqlalchemy. The code:

            bans = Ban.query.filter(Ban.banend >= datetime.now()).all()
            print(bans)
            for ban in bans:
                db.session.delete(ban)
            
            db.session.commit()
            print("Deleted all bans that needed to be deleted")

The model:

class Ban(db.Model, JsonModel):
    ...
    banend = db.Column(db.DateTime, nullable=True)
    ...

There is no error but it doesn't get the items it needs to.

If banend is a date which the ban ends. I believe the logic should be

bans = Ban.query.filter(Ban.banend <= datetime.now()).all() 

This will return all records where banend is BEFORE the current date (aka they should have expired).

If I am misunderstanding could you edit your question to include the items which are (incorrectly) returned?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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