3: Manipulating a Database with SQLAlchemy
Activity 5: Creating a User and a Recipe
Solution
- Open the Python console at the bottom of PyCharm and type in the following code to import the necessary modules and classes:
from app import * from models.user import User from models.recipe import Recipe app = create_app()
- Create a
user
object and save that to the database by typing in the following code in the Python console:user = User(username='peter', email='peter@gmail.com', password='WkQa') db.session.add(user) db.session.commit()
- Next, we will create two recipes using the following code. One thing to note is the fact that the
user_id
attribute of the recipe is set touser.id
. This is to indicate that the recipe was created by the userPeter
:carbonara = Recipe(name='Carbonara', description='This is a lovely carbonara recipe', num_of_servings=4, cook_time=50, directions='This is how you make it', user_id=user...