3

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

3
  • 1
    Try this? Idea.query.filter(Idea.time >= start, Idea.time <= end).all() Commented Mar 23, 2015 at 23:50
  • @AlexanderChen This gives the error "StatementError: SQLite DateTime type only accepts Python datetime and date objects as input. (original cause: TypeError: SQLite DateTime type only accepts Python datetime and date objects as input.) u'SELECT idea.id AS idea_id, idea.user_id AS idea_user_id, idea.title AS idea_title, idea.edited AS idea_edited, idea.time AS idea_time, idea.post AS idea_post, idea.category AS idea_category, idea.deleted AS idea_deleted, idea.votes AS idea_votes \nFROM idea \nWHERE idea.time >= ? AND idea.time <= ?' [immutabledict({})]" Commented Mar 23, 2015 at 23:51
  • 1
    Okay, now try this: from datetime import datetime and datetime.strptime(start, "%d%m%y") for start and end (and also the above). Commented Mar 23, 2015 at 23:54

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Another possible way is just Idea.time.date()
0

Try to use the func.DATE() function:

from sqlalchemy import func

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

refer to similar issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.