CRUD

In every storage mechanism for data, there are four basic types of functions: Create, Read, Update, and Delete (CRUD). These allow all the basic ways of manipulating and viewing data needed for our web apps. To use these functions, we will use an object on the database named the session. Sessions will be explained later in the chapter, but for now, think of them as a storage location for all of our changes to the database.

Creating models

To create a new row in your database using our models, add the model to the session and commit objects. Adding an object to the session marks its changes for saving, and committing is when the session is saved to the database as follows:

>>> user = User(username='fake_name')
>>> db.session.add(user)
>>> db.session.commit() ...

Get Mastering Flask now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.