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

Chris Grazioli
Chris Grazioli
31,225 Points

What am I doing wrong searching the python database, I can't get this code challenge to work properly

from models import Challenge

def create_challenge(name, language, steps=1): Challenge.create(name=name, language=language, steps=steps ) def search_challenges(name, language): challenges=Challenge.select() for challenge in challenges: return challenge.where(name==name, language==language)

crud.py
from models import Challenge


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

3 Answers

Chris Grazioli
Chris Grazioli
31,225 Points

@Michael De La Cruz Thanks a lot, that did help.

But one thing I'm not understanding is where you have to call the object over and over.

Like when you call the Object "Challenge".select().where() Then inside the where you have to refer to the object again through dot syntax and chaining such as ("Challenge".name.contains(name), "Challenge".language==language) Shouldn't that be intuitive since you are in fact referring to the "Challenge" object from the start? Is there any scenario you could think of that would explain why/when you would select everything from an object and then use .where() or .contains() where you wouldn't be referring to an element from that same object?

return Challenge.select().where(Challenge.name.contains(name), Challenge.language==language)
Michael De La Cruz
Michael De La Cruz
10,800 Points

Hello Chris, inside the function, the task is asking us that we need to select and also return all the challenges that match the two criteria without using and or &; so what we could do after defining the function is just simply separate the two conditions with a comma. such as

return Challenge.select().where(Challenge.name.contains(name), Challenge.language==language)

Hope this helps!

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hi Michael,

Just to let you know, I have changed your comment into an answer for readability.

;) Vitto

Michael De La Cruz
Michael De La Cruz
10,800 Points

Hey Chris, I am not sure if this is what you was talking about but just in case we can do it in a another way. For instance..

def search_challenges(name, language):

     challenges = Challenge.select() 

     challenges = challenges.where((Challenge.name==name) | (Challenge.language==language))

     return challenges 

So here we are first selecting all the challenges. Then we filter them by using the name and language arguments that were given. Afterwards, we return the result of the filtered challenges.

I am assuming the way the challenge is worded is a bit of a throw off for some people since they are asking you that "You don't need boolean and or binary & for this, just put both conditions in your where()"