How to Limit the Number of Results Returned From a Database Table with Python in Django 


               


How to Limit the Number of Results Returned From a Database Table with Python in Django



Python


In this article, we show how to limit the number of results returned from a database table with Python in Django.

So, say you only want the top 3 results returned from the database table. Or just the top 5 results returned. Or the top 10 results returned.

This can be done pretty easily in Django.

In SQL language, if you're familiar with it, you have the LIMIT function and then after the keyword LIMIT, you can specify how many results to be returned.

In Django, you specify this with slicing. So if you returned the first 5 results, you would have, [:5]

The whole line would be the following below.



Let's go through a full example so you can see how this actually works in code.

Let's imagine we have a database of Toy objects that represents toys that we, as a company, have in inventory.

It's a whole list of toy.

We want to return the 5 cheapest toys in the whole database.

This is common, like a customer searching for the cheapest item, maybe he or she is on a budget or whatever.

In this case, you would have the following line shown below.



So now we create a variable called sortedobjects and get all the objects from the DatabaseTable and sort them according to price. We then take the top 5 results, representing the 5 cheapest items in the database table.

The entire views.py file is shown below for a database table named Toy.



So we import the Toy database table. We then all objects from the database table and sort them according to price. [:5] gets the top 5 results after the results are sorted from lowest to highest price.

We then create a context dictionary, passing in the variable, sortedprice. And then we render it into the toys.html template file.


Custom Function

Just to show you a custom way of doing this, let's build a custom function that does the same as the code above.

This is shown in the code below.

So the following is our views.py file.



So, in this code, what we do is we get all of the objects in the entire database.

We then have a function that sorts the database according to the price column of the Toy database table and stores the value in the sorted_toys_price.

We then set the sorted_toys_price variable to, sorted_toys_price[:5]

This gives us the five cheapest toys in the entire database.

If we had the following line, alltoys= Toy.objects.all()[:5] and then sorted the function, this would not work. It only get the 5 top results in its current state and sort those according to price. With the code above, you can get the cheapest toys, not in the first 5 rows, but the entire database table.

If you just want to return the top 5 results in the database with no sorting at all, then you would use the following line, alltoys= Toy.objects.all()[:5]

We then pass this variable into the template file, where we can then output the data in a table format.

In this code, I purposely pass in alltoys into the context variable, so you can see how this works. Notice how alltoys isn't truncated at all, while sorted_toys_price is truncated.

Putting both variables into a table format, I get the following output, shown below.

Limiting the number of results returned from a database table in Django



So you can see how it returns the 5 cheapest items in the entire database table.

And this how you can limit the number of results returned from a database table with Python in Django.


Related Resources

How to Create Dynamic URLs in Django

How to Create a Video Uploader with Python in Django

How to Create an Image Uploader with Python in Django



HTML Comment Box is loading comments...