Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Retrieve Data | Working with Database
Django: First Dive
course content

Conteúdo do Curso

Django: First Dive

Django: First Dive

1. Get Started
2. Write the First Page
3. Models
4. Working with Database
5. Templates
6. Request Handling

bookRetrieve Data

To retrieve data from the database, you need to use the Manager.
This manager is available automatically if your IDE has a Django support. In another case, you need to define one more class attribute in your model.

Our Post class should look like this:

There are many different ways to retrieve data from the database.
We will consider the main two.

Retrieve all records

To retrieve data from the table (Model class), you need to use the all() manager method.

In this case, we receive the iterable QuerySet.

In Django, a QuerySet is a collection of database objects (model instances). It represents the results of a query and allows you to retrieve, filter, update, and delete data from your database tables using Python code. It provides methods and properties that help you interact with the data, such as retrieving objects, applying filters, sorting, and performing aggregations. QuerySet offers a powerful and convenient way to work with your database in Django applications.

Let's iterate on the received Post QuerySet and display information on the HTML page.

The provided code defines a View function named post_list that handles an HTTP request. Here's a breakdown of what the code does:

  • Inside the function, it retrieves all Post objects from the database using Post.objects.all(). This creates a QuerySet that contains all the Post instances.
  • An empty string variable named html is initialized. This variable will store the HTML content generated dynamically based on the retrieved Post objects.
  • A for loop is used to iterate over each post in the posts QuerySet. For each post, the code appends HTML content to the html string variable. It creates an <h1> heading with the post's title, a <p> paragraph with the post's text, and another <p> paragraph displaying the post's id.

In summary, this code fetches all the Post objects from the database and generates an HTML string that includes the title, text, and ID of each post. The resulting HTML is then sent as the response to the user's request.

Let's connect this View to the URL pattern inside the urls.py file.

Now, our page is available by the http://127.0.0.1:8000/posts/ URL address.

Retrieve one object

To retrieve just one record from the database, you can use the get() manager method.

A View function should look like this:

The main problem is a pk argument. To receive this argument, we need to connect a specific URL pattern:

The pattern is defined as "posts/<int:pk>/", which represents a URL pattern.

<int:pk> is a path converter that captures an integer value from the URL and assigns it to a variable named pk. This variable can then be used to identify a specific post.

The taken pk value via URL address is passed to the view function as the pk argument, and we can use this argument for the get() method.

Note

  • You can receive other data types via URL address.
  • Any name can replace the pk argument name, but the name should be the same as the View function argument name.

Let's look at the result in the browser.

1. How to extract all records from the database?
2. How to extract one record from the database?
3. Which syntax is used for the URL path to receive the `pk`?
How to extract all records from the database?

How to extract all records from the database?

Selecione a resposta correta

How to extract one record from the database?

How to extract one record from the database?

Selecione a resposta correta

Which syntax is used for the URL path to receive the `pk`?

Which syntax is used for the URL path to receive the pk?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2
some-alt