Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,107 Points

problem with search_challenges (receiving AttributeError)

I keep receiving the error: AttributeError: 'NoneType' object has no attribute 'count'... I've researched the error message to no avail. I've also looked at the documentation for the select() and where() methods, and feel like i'm calling them correctly. Thanks in advance for any help.

my code:

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()
  challenges.where(Challenge.name.contains(name) & Challenge.language == language)

2 Answers

Yes, the word 'return' is needed:

from models import Challenge

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


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

Of course, this didn't help me, because by the time I found this thread I had already moved on to the next challenge:

http://teamtreehouse.com/library/using-databases-in-python/gettin-crudy-with-it/crud-delete-function

Challenge Task 1 of 1

Create a function named delete_challenge that takes a Challenge as an argument. Delete the specified Challenge. Your function shouldn't return anything.

Watch the video over and over again:

http://teamtreehouse.com/library/using-databases-in-python/gettin-crudy-with-it/delete-an-entry

..at about time index 2:43 I found (and re-typed since there is no way to OCR text from the videos):

def delete_entry(entry):
  entry.delete_instance()

..which I eventually 'translated' to:

def delete_challenge(Challenge):
  Challenge.delete_instance()

Note: I wish this challenge wasn't so new...couldn't find many threads on it..

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You probably want to lowercase Challenge in your delete_challenge function so you don't confuse it with the class.