Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Retrieve Query | Queries
course content

Course Content

Django ORM Ninja: Advanced Techniques for Developers

Retrieve QueryRetrieve 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?

Select the correct answer

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

Select the correct answer

question-icon

Which query combines multiple filters in Django?

Select a few correct answers

What does the get() method do in Django?

Select the correct answer

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

Select the correct answer

Everything was clear?

Section 3. Chapter 2
course content

Course Content

Django ORM Ninja: Advanced Techniques for Developers

Retrieve QueryRetrieve 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?

Select the correct answer

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

Select the correct answer

question-icon

Which query combines multiple filters in Django?

Select a few correct answers

What does the get() method do in Django?

Select the correct answer

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

Select the correct answer

Everything was clear?

Section 3. Chapter 2
some-alt