Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Dates in Flask-SQLAlchemy

I've been trying to compare dates in a query given to SQLALchemy as follows:

start = time.strptime(start, "%d%m%y")
end = time.strptime(end, "%d%m%y")
list_ideas = Idea.query.filter(time >= start, time <= end).all()

However, this does not return results regardless of dates given (where Idea.time = db.Column(db.DateTime, default=db.func.now())). I've searched through some other answers regarding this topic and from what I have gathered, I am not making the same mistakes.

In addition, changing the query to Idea.query.filter(time >= start, time <= end, deleted=False).all() gives the error:

TypeError: filter() got an unexpected keyword argument 'deleted'

Any pointers would be appreciated.

--
EDIT: I noticed that I was using import time, which may have caused the error. However, after changing it to from time import strptime, I now experience the error:

NameError: global name 'time' is not defined

like image 826
n0shadow Avatar asked Feb 23 '26 05:02

n0shadow


1 Answers

try using the datetime module

from datetime import datetime

Idea.query.filter(Idea.time >= datetime.strptime(start, '%Y-%m-%d'),
                  Idea.time <= datetime.strptime(end, '%Y-%m-%d')).all()

you will just need to change the pattern to match what ever format you are using

like image 195
Michael Moura Avatar answered Feb 26 '26 07:02

Michael Moura



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!