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 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)

crud.py
from models import Challenge


def create_challenge(name, language, steps=1):
    Challenge.create(name=name,
                     language=language,
                     steps=steps)

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

1 Answer

Megan Amendola
seal-mask
STAFF
.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
seal-mask
.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.