Declaring Models in Flask

The Flask community provides the “Flask-SQL Alchemy” library/extension which is the go-to library for declaring models in Flask. It is a wrapper around the “SQL Alchemy” library with added capabilities to handle the details related to responses and requests so that you don’t have to worry about that. Before proceeding any further, we need to have the following installations –

Installations required

Flask doesn’t support ORM, but with the help of flask-sqlalchemy, we can achieve the ORM functionalities. Once Python is installed, we can use a package manager such as pip to install the rest with this command:

pip install flask-sqlalchemy

Declaring Models in Flask

Models are used in Flask to conveniently handle interactions with databases (like SQL, SQLite, etc.) using ORM (Object Relational Mapping). This article describes what is ORM, how to declare models in Flask, and finally a simple example Flask application. It assumes basic familiarity with Flask and Python programming languages.

Similar Reads

What is ORM in Python Flask?

ORM (Object Relational Mapping) is a programming technique which lets the programmer to write code using the Object-Oriented features of a language to interact with a database....

Declaring Models in Flask

The Flask community provides the “Flask-SQL Alchemy” library/extension which is the go-to library for declaring models in Flask. It is a wrapper around the “SQL Alchemy” library with added capabilities to handle the details related to responses and requests so that you don’t have to worry about that. Before proceeding any further, we need to have the following installations –...

Declaring Models in Flask with Flask-SQL Alchemy

This section contains the step-by-step explanation of the code. After doing the necessary imports, we define the create_app function which returns the flask app (the app-factory method of creating flask apps). We create the app and define everything inside this function since this app is small. If it is large, then in-practice, one would generally create multi-file definitions and then add them to the app inside this function by using flask features such as blueprints. Here is the explanation of the code inside this function –...

Flask app using Models

...

Conclusion

...

Contact Us