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: Advanced Techniques for Developers
course content

Conteúdo do Curso

Django ORM Ninja: Advanced Techniques for Developers

Django ORM Ninja: Advanced Techniques for Developers

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

bookRetrieve 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. Which 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?

Selecione a resposta correta

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

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

Selecione a resposta correta

Which query combines multiple filters in Django?

Which query combines multiple filters in Django?

Selecione algumas respostas corretas

What does the get() method do in Django?

What does the get() method do in Django?

Selecione a resposta correta

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?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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