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 Flask REST API Resourceful Blueprints Ingredient Resource

Kennedy Malonga
Kennedy Malonga
18,148 Points

i still dont understand this for a while now

i tried different ways it still left me struggling

models.py
import datetime

from peewee import *

DATABASE = SqliteDatabase('recipes.db')

class Recipe(Model):
    name = CharField()
    created_at = DateTimeField(default=datetime.datetime.now)

    class Meta:
        database = DATABASE

class Ingredient(Model):
    recipe = ForeignKeyField(name, description, quantity, measurement_type)
    quantity = DecimalField()

    class Meta:
        database = DATABASE




# TODO: Ingredient model
# name - string (e.g. "carrots")
# description - string (e.g. "chopped")
# quantity - decimal (e.g. ".25")
# measurement_type - string (e.g. "cups")
# recipe - foreign key
resources/ingredients.py
from flask.ext.restful import Resource

import models

1 Answer

class Ingredient(Model):
    recipe = ForeignKeyField(name, description, quantity, measurement_type)
    quantity = DecimalField()

    class Meta:
        database = DATABASE




# TODO: Ingredient model
# name - string (e.g. "carrots")
# description - string (e.g. "chopped")
# quantity - decimal (e.g. ".25")
# measurement_type - string (e.g. "cups")
# recipe - foreign key

Instead of recipe = ForeignKeyField(name, description, quantity, measurement_type), try recipe = ForeignKeyField(Recipe) to link to the Recipe model.

In addition to the recipe and quantity fields you already have, you will also need to add fields for name, description, and measurement_type. Since they will be strings, you can use CharField() to initialize them, such as name = CharField().