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

Andrei Oprescu
Andrei Oprescu
9,547 Points

using .where() in a challenge

Hello!

I have a challenge with a question like this:

Create a function named search_challenges that takes two arguments, name and language. Return all Challenges where the name field contains name argument and the language field is equal to the language argument. Use == for equality. You don't need boolean and or binary & for this, just put both conditions in your where().

The code that I created for this challenge is at the bottom of this post.

Can someone tell me hoe to implement .where() in my code and how to fix it?

Thanks!

Andrei

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):
    challenges = Challenge.select()
    if name, language == challenges.where(name, language):
      return challenge

2 Answers

Steven Parker
Steven Parker
231,236 Points

Here's a few hints:

  • you can chain .where on to the end of the select()
  • the arguments to .where should be complete comparison expressions
  • you won't need a "if" statement
Andrei Oprescu
Andrei Oprescu
9,547 Points

Hi again!

I took your hints to what my code must look like and I have come to this code:

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

Can you tell me why this code doesn't work?

Thanks!

Andrei

Steven Parker
Steven Parker
231,236 Points

You're really close now! But the challenge doesn't want only cases where the name is an exact match, the instructions said "...where the name field contains name argument".

That's a bit of a hint in itself, since strings have a "contains" method that could be really handy here.

Andrei Oprescu
Andrei Oprescu
9,547 Points

Hi!

Thank you for your hint! That helped me a lot to understand the use of .where() and to understand how to do this challenge.

Thanks!

Andrei