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: Create Function

Devin Scheu
Devin Scheu
66,191 Points

PeeWee Python Help

Question: Create a function named create_challenge() that takes name, language, and steps arguments. Steps should be optional, so give it a default value of 1. Create a Challenge from the arguments. create_challenge should not return anything.

Code:

crud.py
from peewee import *
from models import Challenge
def create_challenge(name, language, steps):
    name = CharField(max_length=100)
    language = CharField(max_length=100)
    steps = IntegerField(default=1)

1 Answer

Dan Johnson
Dan Johnson
40,532 Points

You don't need to define the model here, just create it using the Challenge model that is being imported. Since Challenge extends Model, you can call the create method on it.

Devin Scheu
Devin Scheu
66,191 Points

So what would this look like in code?

Dan Johnson
Dan Johnson
40,532 Points

From the peewee docs: http://peewee.readthedocs.org/en/latest/peewee/api.html#Model.create

Note that you won't need to capture the return for this challenge.