Most modern web applications are powered by a REST API under the hood. That way, developers can separate the front-end code from the back-end logic, and users can interact with the interface dynamically. In this three-part tutorial series, you’re building a REST API with the Flask web framework.
You’ve created a foundation with a basic Flask project and added endpoints, which you connected to a SQLite database. You’re also testing your API with Swagger UI API documentation that you’re building along the way.
In the third part of this tutorial series, you’ll learn how to:
- Work with multiple tables in a database
- Create one-to-many fields in your database
- Manage relationships with SQLAlchemy
- Leverage nested schemas with Marshmallow
- Display related objects in the front end
You can download the code for the third part of this project by clicking the link below:
Source Code:Click here to download the free source code that you’ll use to finish building a REST API with the Flask web framework.
Demo
In this three-part tutorial series, you’re building a REST API to keep track of notes for people who may visit you throughout the year. You’ll create people like the Tooth Fairy, the Easter Bunny, and Knecht Ruprecht.
Ideally, you want to be on good terms with all three of them. That’s why you’ll send them notes, to increase the chance of getting valuable gifts from them.
In this tutorial, you’ll expand your programming tool belt further. You’ll learn how to create hierarchical data structures represented as one-to-many relationships by SQLAlchemy. In addition, you’ll also extend the REST API that you’ve already built to create, read, update, and delete notes for a person:
It’s time to finish this three-part tutorial series by creating relationships between people and notes!
Planning Part Three
In part one of this series, you built your REST API. Working through part two, you connected your REST API to a database. That way, your Flask application can make changes to existing data and create new data that persists even when you restart your app server.
So far, you’ve added the ability to save changes made through the REST API to a database using SQLAlchemy and learned how to serialize that data for the REST API using Marshmallow.
Currently, the people.db
database only contains people data. In this part of the series, you’ll add a new table to store notes. To connect notes to a person, you’ll create relationships between the entries of the person
table and the note
table in your database.
You’ll bootstrap people.db
with a build_database.py
script that contains the necessary people and notes data for you.
Here’s an excerpt of the dataset that you’ll work with:
PEOPLE_NOTES=[{"lname":"Fairy","fname":"Tooth","notes":[("I brush my teeth after each meal.","2022-01-06 17:10:24"),("The other day a friend said I have big teeth.","2022-03-05 22:17:54"),("Do you pay per gram?","2022-03-05 22:18:10"),],},# ...]
You’ll learn how to adjust your SQLite database to implement relationships. After that, you’ll be able to translate the PEOPLE_NOTES
dictionary into data that conforms with your database structure.
Finally, you’ll show the content of your database on the home page of your app and use your Flask REST API to add, update, and delete notes that you’re writing for people.
Getting Started
Ideally, you followed the first part and the second part of this tutorial series before continuing with the third part, which you’re reading right now. Alternatively, you can also download the source code from part two by clicking the link below:
Source Code:Click here to download the free source code that you’ll use to continue building a REST API with the Flask web framework.
If you downloaded the source code from the link above, then make sure to follow the installation instructions within the provided README.md
file.
Before you continue with the tutorial, verify that your folder structure looks like this:
Read the full article at https://realpython.com/flask-connexion-rest-api-part-3/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]