Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Retrieve Query | Queries
Django ORM Ninja: Técnicas Avanzadas para Desarrolladores
course content

Contenido del Curso

Django ORM Ninja: Técnicas Avanzadas para Desarrolladores

Django ORM Ninja: Técnicas Avanzadas para Desarrolladores

1. Introduction to Django ORM
2. Models, datatypes, and fields
3. Queries
4. Relations
5. Complex Queries
6. Advanced

book
Retrieve Query

We already know how to retrieve all instances using Author.objects.all(). In this chapter, we'll learn how to obtain a specific instance or filter multiple instances based on certain attributes.

filter()

This query returns a new QuerySet containing objects that match the given lookup parameters. In our example, it will return only one Author with id=3.

The equivalent SQL command is SELECT * FROM Author WHERE last_name = 'Clear';

exclude()

Conversely, exclude() returns a QuerySet with objects that do not match the specified parameters. Here, it will include all Authors whose last name isn't 'Clear'.

The equivalent SQL command is SELECT * FROM author WHERE last_name != 'Clear';

Combine filters

We can also combine filters. For instance, the query above demonstrates how to use multiple lookups, which we will explore more in the next sections.

The equivalent SQL command is SELECT * FROM author WHERE last_name LIKE 'C%' AND first_name != 'Ronald';

get()

To retrieve a specific object, we use the get method, where pk is a "primary key", and it's the same as id.

The equivalent SQL command is SELECT * FROM author WHERE id = 1;

Unlike get(), slicing with filter() doesn't raise an exception if there are no matching results; it simply returns an empty QuerySet.

1. What does Author.objects.filter(last_name='Clear') return in Django?
2. How does the exclude() method in Django differ from filter()?
3. hich query combines multiple filters in Django?
4. What does the get() method do in Django?
5. How does Django's filter() method behave when there are no matching results?
What does Author.objects.filter(last_name='Clear') return in Django?

What does Author.objects.filter(last_name='Clear') return in Django?

Selecciona la respuesta correcta

How does the exclude() method in Django differ from filter()?

How does the exclude() method in Django differ from filter()?

Selecciona la respuesta correcta

hich query combines multiple filters in Django?

hich query combines multiple filters in Django?

Selecciona unas respuestas correctas

What does the get() method do in Django?

What does the get() method do in Django?

Selecciona la respuesta correcta

How does Django's filter() method behave when there are no matching results?

How does Django's filter() method behave when there are no matching results?

Selecciona la respuesta correcta

¿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