Contenido del Curso
Flask Intensive Course: Web Development with Python
Flask Intensive Course: Web Development with Python
Creating Flask Model
Before implementing the Model, you should know the Object Oriented Programming (OOP).
Create:
We need to inherit our model from db.Model
.
-
id
has a special second parameter, which means that thisid
is going to be the main distinguisher between different recipes because this field is always unique; -
title
has 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; -
description
has type Text, and it goes for as long as it once, and this field is required too; -
author
has the same type astitle
; -
date_posted
is 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:
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.
¡Gracias por tus comentarios!