Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Read and Create Queries | Implement CRUD
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

bookRead and Create Queries

We'll start by setting up everything in a Python environment, and then we'll seamlessly integrate it into our code. Let's enhance our database by adding a few entries to it. To do this, we'll need to import the models and continue our work in the Python environment. Type into your Terminal:

Or open Python Console. Let's practice with queries to the database.

Next query will implement READ functionality:

This command will get all the Recipes from the table. But we received an empty list because we haven't put anything in the database yet. So, we want to add a new model. It is CREATE functionality.

Now, let's repeat the previous command, Recipe.query.all(). And we have the first entry in our database. Repeat the second command and add Recipe 2. Let's do it through variables.

Also, we can READ from the database in different ways.

We can slice the list with our Recipes:

We will get a second Recipe.

And we can receive a specific field:

We can read only the first one, too. It just gets the first one from the entire list:

Also, you can assign the result to some variable, and it will work accordingly:

We can filter by things. We can actually call the .filter_by() method and specify what we are going to filter so far as we filter by title, we can filter by any model's attribute:

So, you can see only Recipe 1 because it is related to our query condition.

There is also a get method. What does it do? It gets whichever object by its id or other unique attribute:

If we go out of range, nothing breaks. But if you print it out, it will return None. It's just an empty thing.

However, you might encounter some warnings with this method. In such cases, it's better to use the following approach:

There is another get_or_404() method. We will use it a little bit later.

There are pretty much things we will use in our application.

And, lastly, very important different kinds of READ you can look at the official documentation if you want to know more.

We made temporary entries to our database. To save our examples, we need to commit it by command:

Now, the changes are happening in the DB file, and if we completely restart the entire computer, close all these, or have a different terminal session, this data will be preserved in the file even if we move this file to a different computer.

Let's hook up this database to our FrontEnd.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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