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!

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 trial

Python Using Databases in Python Gettin' CRUD-y With It CRUD: Search Function

How do I use the 'where' clause?

How do I design my query if 'Challenge' has no attribute 'where' (i.e. Challenge.where() is giving a syntax error)

from models import Challenge

def create_challenge(name, language, steps=1):

def search_challenges(name, language):
    search = Challenge.select()
    return search where(name==name, language==language)

1 Answer

Megan Amendola
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

At about 6:54 in the video before is where he talks about .where().

.where() can either attached on to the end of Challenge.select().where(logic here) or since you hade it a variable, you can use the variable search.where(logic here).

Here are some examples from the documentation as well: http://docs.peewee-orm.com/en/latest/peewee/querying.html#filtering-records

I've corrected my query (I used 'return Challenge.select().where(Challenge.name==name, Challenge.language==language)') but the error states that the line did not return the correct number of records

Megan Amendola
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

The challenge is asking for where the name field contains name argument. Right now you are checking for when the field is equal == to the name argument. Call .contains(name) on Challenge.name to check if it contains the name argument instead.