Book Image

Learning Flask Framework

Book Image

Learning Flask Framework

Overview of this book

Table of Contents (17 chapters)
Learning Flask Framework
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the Entry model


A model is the data representation of a table of data that we want to store in the database. These models have attributes called columns that represent the data items in the data. So, if we were creating a Person model, we might have columns for storing the first and last name, date of birth, home address, hair color, and so on. Since we are interested in creating a model to represent blog entries, we will have columns for things like the title and body content.

Note

Note that we don't say a People model or Entries model – models are singular even though they commonly represent many different objects.

With SQLAlchemy, creating a model is as easy as defining a class and specifying a number of attributes assigned to that class. Let's start with a very basic model for our blog entries. Create a new file named models.py in the blog project's app/ directory and enter the following code:

import datetime, re
from app import db

def slugify(s):
    return re.sub('[^\w]+', ...