Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating Flask Model | Database
Flask Intensive Course: Web Development with Python
course content

Contenido del Curso

Flask Intensive Course: Web Development with Python

Flask Intensive Course: Web Development with Python

1. Introduction to Flask
2. Database
3. Implement CRUD
4. FrontEnd Design

bookCreating 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 this id 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 as title;

  • 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.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
We're sorry to hear that something went wrong. What happened?
some-alt