Flask Series: Model

Monday, Jul 13, 2015 19:12 · 490 words · 3 minutes read

Flask Series

  1. Prepare the Environment
  2. Structure the Application
  3. Application Configuration
  4. Templating
  5. Model
  6. Testing
  7. Views and Web Forms
  8. Error Management
  9. Security
  10. Optimizations
  11. Healthcheck and Monitoring
  12. Internationalization
  13. Deployment

Working with data is one of the most important aspects in the software development.

Object Relational Mapping

Flask provides the developers with the power and flexibility of the SQL Alchemy ORM to manage and query the application data. ORM stands for Object Relational Mapper, it is a tool that allows developers to store and retrieve data using object oriented approaches and solving the object-relational impedance mismatch.

“Object-Relational Impedance Mismatch” in Wikipedia: a set of conceptual and technical difficulties that are often encountered when a relational database management system (RDBMS) is being used by a program written in an object-oriented programming language or style, particularly when objects or class definitions are mapped in a straightforward way to database tables or relational schema.

Flask-SQLAlchemy

Flask-SQLAlchemy is an extension that provides support for SQLAlchemy ORM in the Flask framework.

Configuration

The extension uses the SQLALCHEMY_DATABASE_URI configuration parameter to connect to the underlying data store. It supplies the connection URI format to the SQL Alchemy, by using the following pattern:

Example connection URI formats for connecting to various backends can be found here.

I have updated the config.py module to introduce the needed SQL Alchemy configuration data:

SQLite is my database engine of choice, it is a embedded database.

Note: You should correct the SQLALCHEMY_DATABASE_URI setting value if you do not work in Windows environment. At the moment a SQLite db file will created under the C:\Temp if missing.

Model

Now I should declare the models that will be used by the Flask Bookshelf application. What I need is a model for the books management and their authors, in the first place.

I have added models.py module under the bookshelf\data that will contain all of my application model.

Here I am using a base class for declarative class definitions – db.Model, which is used to declare the application models. Detailed information about this approach can be found here.

The table name of the class underlying table can explicitly specified using the following approach:

Using Column I am able to declare the columns for my model and their corresponding properties.

Example:

The complete column’s types list can be found here.

Relationships are declared with the relationship function:

Here I have declared the foreign key for the relationship above:

Initialization

Now that I have the model in place, I would like to initialize the application with the declared database setup.

CRUD Operations

Insert a Record

To insert a new record using the SQL Alchemy you should follow these three steps:

  1. create an object
  2. add the object to the session
  3. commit the session

Delete a Record

Query Data

Further information can be found here.

In the next blog post I will describe how to test your Flask application.

The complete demo application, described in this blog post, can be found here.
comments powered by Disqus

Buy Me A Coffee