How to Filter Items From Query Set in Descending Order in Django

Salman Mehmood Feb 02, 2024
How to Filter Items From Query Set in Descending Order in Django

In this tutorial, we will learn how to filter out the items of the query set in descending order with the order_by() method in Django.

Use the order_by() Method to Filter Items From Query Set With Descending Order in Django

The order_by() method orders your results. The default order was the order when we inserted them into the database.

Let us see what it looks like to get the default ordering. First, let us get all objects.

simple.objects.all()

Output:

<QuerySet [<Simple:www.example.com>,<simple:www.yahoo.com>,<www.example.com>,<www.example.com>,<facebook.com>,<apple.com>]>

We would use order_by() if we want a different order. Instead of all(), we can use order_by().

It is going to operate on all the results. If we had a filter before this, it would run first and then the order_by().

Now, we will pass a string to specify what will be ordered. For example, if we filter it with order_by("id"), we get the same result because it starts with the smallest id and works its way up.

See the code below.

simple.objects.order_by("id")

Output:

<QuerySet [<Simple:www.example.com>,<simple:www.yahoo.com>,<www.example.com>,<example.com>,<facebook.com>,<apple.com>]>

We use a - in front of id to reverse it, so instead of starting with the smallest, it will begin with the largest id. When we run it, we see that www.apple.com is the first item of our query set.

simple.objects.order_by("-id")

Output:

<QuerySet [<apple.com>,<facebook.com>,<example.com>,<www.example.com>,<simple:www.yahoo.com>,<Simple:www.example.com>]>

We can also use order_by() in the other columns.

Suppose we have the url column. If we order it by the url, we can use the following code.

simple.objects.order_by("url")

Output:

<QuerySet [<apple.com>,<Simple:example.com>,<facebook.com>,<www.example.com>,<www.example.com>,<simple:www.yahoo.com>]>

Since it is a string field, it will order it alphabetically. We can filter it in descending order by simply putting - in front.

In our query set, we get the first item, www.yahoo.com.

simple.objects.order_by("-url")

Output:

<QuerySet [<simple:www.yahoo.com>,<www.example.com>,<www.example.com>,<facebook.com>,<Simple:example.com>,<apple.com>]>

Usually, we would have multiple order_by() when potentially two columns could have the exact ordering. Let’s look at the number column, which has two 10 numbers and some different numbers.

simple.objects.order_by("number")

Output:

<QuerySet [<apple.com>,<Simple:example.com>,<facebook.com>,<www.example.com>,<www.example.com>,<simple:www.yahoo.com>]>

We can see that apple.com is first because the number is “2” follows the same row.

We can append another option to order_by() if we want to force a more strict ordering. First, we will order by the number, and then by the url.

simple.objects.order_by("number", "url")

If we want to limit the results, say the first three, we can use the following syntax.

simple.objects.order_by("-id")[:3]

Output:

<QuerySet [<apple.com>,<facebook.com>,<example.com>]>

Now, we only have three items of the query set in descending order.

Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn