Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndy McDonald
Python Development Techdegree Graduate 13,801 PointsHow to identify empty query object when empty query is truthy?
Working on the project and if the app searches for a book thats not in the database I would like the app to say so. The best way I know how to do so is like so:
def delete():
todel = input("What's the title of the book you want to delete?")
dellie = models.session.query(models.Book).filter_by(title=todel)
if dellie:
for book in dellie:
models.session.delete(book)
models.session.commit()
print('Alright that book is gone. Now what?')
else:
print('Cant find a book by that name. Try again.')
However, when I input a book title that does not exist it returns a query object that is truthy. Any suggestions?
[MOD: added ```python formatting -cf]
1 Answer
Chris Freeman
Treehouse Moderator 68,427 PointsHey Andy McDonald, then query method count()
gives the size of the query result.
if dellie.count() > 0:
# OR in truthy fashion
if dellie.count():
Post back if you need more help. Good luck!!!