Creating Flask Model
Before implementing the Model, you should know the Object Oriented Programming (OOP).
Create:
# main.py(continuation)
from datetime import datetime
class Recipe(db.Model):
__tablename__ = "recipes"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False, unique=True)
description = db.Column(db.Text, nullable=False)
author = db.Column(db.String(50))
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
def __repr__(self):
return "Recipe" + str(self.id)
We need to inherit our model from db.Model.
-
idhas a special second parameter, which means that thisidis going to be the main distinguisher between different recipes because this field is always unique; -
titlehas datatype String and we limit this to a certain amount of characters. The second parameter means that this field is required and cannot be Null or empty; -
descriptionhas type Text, and it goes for as long as it once, and this field is required too; -
authorhas the same type astitle; -
date_postedis a special field with autocomplete, and do not forget to import the datetime module; -
The method
__repr__in our model is going to print out a representation of entries in some different way.
So, we added the SQLAlchemy library to work with our database and defined our database as well as a place to store it. We finished our model. So, let's go down to our Terminal and just start the Python environment by calling python or python3:
Import database:
The next command goes through our models and configurations, and it's going to create the tables in the database with our models in mind.
But if you receive an error message like:
Please run these commands:
>>> from main import app
>>> app_ctx = app.app_context()
>>> app_ctx.push()
>>> db.create_all()
>>> app_ctx.pop()
We have set up an applicational context and run a command to create a database file inside the context. You can see that the new file was created in the same directory as our Flask application.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.7
Creating Flask Model
Swipe to show menu
Before implementing the Model, you should know the Object Oriented Programming (OOP).
Create:
# main.py(continuation)
from datetime import datetime
class Recipe(db.Model):
__tablename__ = "recipes"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False, unique=True)
description = db.Column(db.Text, nullable=False)
author = db.Column(db.String(50))
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
def __repr__(self):
return "Recipe" + str(self.id)
We need to inherit our model from db.Model.
-
idhas a special second parameter, which means that thisidis going to be the main distinguisher between different recipes because this field is always unique; -
titlehas datatype String and we limit this to a certain amount of characters. The second parameter means that this field is required and cannot be Null or empty; -
descriptionhas type Text, and it goes for as long as it once, and this field is required too; -
authorhas the same type astitle; -
date_postedis a special field with autocomplete, and do not forget to import the datetime module; -
The method
__repr__in our model is going to print out a representation of entries in some different way.
So, we added the SQLAlchemy library to work with our database and defined our database as well as a place to store it. We finished our model. So, let's go down to our Terminal and just start the Python environment by calling python or python3:
Import database:
The next command goes through our models and configurations, and it's going to create the tables in the database with our models in mind.
But if you receive an error message like:
Please run these commands:
>>> from main import app
>>> app_ctx = app.app_context()
>>> app_ctx.push()
>>> db.create_all()
>>> app_ctx.pop()
We have set up an applicational context and run a command to create a database file inside the context. You can see that the new file was created in the same directory as our Flask application.
Thanks for your feedback!