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 PointsWhy am I getting 2 different results with a query depending on whether I run it as a script or in the consel?
Trying to do the first database project without peeking. Im hung up on the edit portion. I cant get the app to return anything to edit. Whats really stopping me from moving forward is the inability to run a query inside the script. When running the following code:
session.query(models.Book).filter_by(title='Mindset')
i return a query object in the consel and dont seem to get anything from the script. The script returns some SQL stuff that appears to have nothing passed in for title.
Side question:
Is there a way to pass a variable to an attribute? For example:
variable = title variable2 = Mindset
session.query(models.Book).filter(models.Book.variable = variable2)
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsHey Andy McDonald, when running in the script, you may be getting the query object. It just looks like "query language" perhaps due to printing it. The print calls the __str__
method of the query object which may not return same value as the __repr__
version that is returned in the REPL.
If query_object
is the result of the query, for the script try:
print(f"There are {query_object.count()} objects in the query result")
for q in query_object:
print(q)
As for using variables in queries, look at this other Treehouse post answer
Post back if you need more help. Good luck!!
Andy McDonald
Python Development Techdegree Graduate 13,801 PointsAndy McDonald
Python Development Techdegree Graduate 13,801 Points.filter() would use python operators such as '=='. Adding that to .filter_by() throws an error in both the consel and script.