Contenu du cours
Professional Web API with Flask
Professional Web API with Flask
Create Models
As we embark on creating a professional application, it's crucial to organize functionality and logic across various packages and files. This methodology not only aids in maintaining a clean codebase but also in simplifying the development process. We have already initiated this structure by segregating the database initialization in db.py and the application launch and configuration settings in app.py.
Creating the Models Package
To further our application's organization, we will now create a new Python package named models. If your IDE allows only folder creation, simply create a folder and include an empty __init__.py file in it to signify it as a package. Within this package, we will create several files for our models: team.py, player.py, user.py, and club.py.
Example Model Creation: Team
Here, I'll remind you how to create models using team.py as an example:
from db import db # Importing our database
class TeamModel(db.Model): # Inheriting our model from db.Model
__tablename__ = "teams" # Specifying the table name
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50), unique=True, nullable=False)
league = db.Column(db.String(50), nullable=False)
club_id = db.Column(db.Integer, nullable=False)
manager = db.Column(db.Integer, nullable=False)
This pattern is repeated for the player and club models.
Player Model:
from db import db
class PlayerModel(db.Model):
__tablename__ = "players"
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(50), nullable=False)
last_name = db.Column(db.String(50), nullable=False)
position = db.Column(db.String(50), nullable=False)
number = db.Column(db.Integer, nullable=False)
team_id = db.Column(db.Integer, nullable=False)
Club Model:
from db import db
class ClubModel(db.Model):
__tablename__ = "clubs"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
country = db.Column(db.String(50), nullable=False)
city = db.Column(db.String(50), nullable=False)
main_stadium = db.Column(db.String(50), nullable=False)
est = db.Column(db.Date, nullable=False)
User Model:
class UserModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=True, nullable=False)
password = db.Column(db.String(80), nullable=False)
full_name = db.Column(db.String(100), nullable=False)
Simplifying Model Imports
To ease the importation of models within our application, we'll declare them in the __init__.py file of the models package:
from models.team import TeamModel
from models.player import PlayerModel
from models.club import ClubModel
from models.user import UserModel
This organization not only streamlines the development process by segregating the database schema into understandable chunks but also facilitates easier management and modification of the application structure. By adopting this modular approach, we ensure that our application remains scalable and maintainable, accommodating future expansions and modifications with ease.
Merci pour vos commentaires !