Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen First Endpoint, Flask-Migration and SQLite Studio | Endpoints with Blueprints and MethodView
Professional Web API with Flask

book
First Endpoint, Flask-Migration and SQLite Studio

Implementing the Club Endpoint

Let's define our first functionality for the Club GET endpoint. We return a ClubModel.query.get_or_404() using ORM, passing the club_id. This simplifies the development process by avoiding raw SQL queries. We import our model from the respective module.

Combining Methods and Schemas

We also need to link our method to the schema we created for data serialization and validation. We use the @blp.response() decorator to return a 200 status code for successful request processing and import ClubSchema from our schemas module.

python
from flask.views import MethodView
from flask_smorest import Blueprint
from models.club import ClubModel
from schemas import ClubSchema


blp = Blueprint("clubs", __name__, description="Operations on the clubs")


@blp.route("/clubs/<int:club_id>")
class Club(MethodView):
@blp.response(200, ClubSchema)
def get(self, club_id):
return ClubModel.query.get_or_404(club_id)

Migrations

From our previous project, we're familiar with migrations and the Flask-Migrate library. We install Flask-Migrate using:

Initializing Migrations and Database Connection

We need to initialize migrations and link our application to the database. In the app.py file, we import Migrate from the flask_migrate library.

At the end of our file, we initialize a new migrate variable to connect our application (app) and the database (db).

python
from flask_migrate import Migrate
...
migrate = Migrate(app, db)

Creating Migration Folders

We're ready to create a folder for our future migrations using the flask db init command. A new folder named migrations appears, containing several files and a versions subfolder.

Using SQLite Studio

Please follow the video to quickly and practically learn how to use the SQLite Studio app.

In the next chapters, we'll explore additional documentation features and testing capabilities for our API and continue defining functionalities for all models.

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 3

Fragen Sie AI

expand
ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt