Django Tutorial: QuerySet Methods 🎯

beginner
13 min

Django Tutorial: QuerySet Methods 🎯

Welcome back to CodeYourCraft! Today, we're diving into QuerySet methods in Django. These methods help you filter, manipulate, and transform the data retrieved from your database. Let's get started!

What are QuerySet Methods? πŸ“

QuerySet methods are functions that are called on QuerySets (objects representing database queries) to modify or extract data from the database. They allow you to perform various operations, such as filtering, sorting, and counting records, without writing raw SQL.

Filtering Data with QuerySet Methods πŸ’‘

The most common QuerySet methods are used for filtering data. Here are some examples:

filter()

The filter() method returns a new QuerySet that includes only the objects that meet the specified condition.

python
# Filter posts with a title containing 'Django' posts = Post.objects.filter(title__contains='Django')

πŸ“ Note: The double underscores (__) are used for field lookup. title__contains means "search the title field for the string 'Django'".

exclude()

The exclude() method works similar to filter(), but it returns a new QuerySet that includes the objects that do not meet the specified condition.

python
# Get posts that are not about Django posts_not_about_django = Post.objects.exclude(title__contains='Django')

Sorting Data with QuerySet Methods πŸ’‘

order_by()

The order_by() method sorts the QuerySet based on the specified fields. By default, it sorts in ascending order (A-Z, 0-9).

python
# Sort posts by publication date latest_posts = Post.objects.order_by('-pub_date')

πŸ“ Note: The - before pub_date sorts in descending order (Z-A, D-A).

Working with Pagination πŸ’‘

Django provides a paginate_queryset() function to simplify pagination.

python
# Paginate the latest posts into 10-item pages page_obj = Paginator(latest_posts, 10) page_1 = page_obj.get_page(1)

In the above example, page_obj is a Paginator object, and page_1 is the first page of the latest posts.

Performing Actions with QuerySet Methods πŸ’‘

count()

The count() method returns the number of objects in the QuerySet.

python
# Count the total number of posts total_posts = Post.objects.count()

update()

The update() method modifies the records in the database that match the specified condition.

python
# Update all posts with 'Django' in the title to have the published attribute set to True Post.objects.filter(title__contains='Django').update(published=True)

delete()

The delete() method removes the matching objects from the database. Be careful with this one!

python
# Delete all posts with 'Django' in the title Post.objects.filter(title__contains='Django').delete()

Quiz Time πŸ’‘

Quick Quiz
Question 1 of 1

What does the `exclude()` method do in Django QuerySets?

That's it for today's lesson on QuerySet methods in Django! These methods will help you perform powerful data manipulations in your projects. Stay tuned for more Django tutorials here at CodeYourCraft! πŸ€–πŸš€