How to Do SELECT MAX in Django?
Last Updated :
15 Aug, 2024
When working with databases in Django, we often need to find the highest value of a specific field in a model. Let's say, find the book with the highest price, the product with the highest discount, etc. This is like doing a SELECT MAX query in SQL. In this article, we'll learn how to find the max value using the `aggregate()` and `order_by()`, methods from the Django Database Function.
Method 1 - Using Django aggregate()
Django provides the aggregate() method, which allows us to perform aggregate queries on a model. To use aggregate(), we need to import the Max aggregation function from django.db.models.
from django.db.models import Max
Code Example:
Let's take an example, we have a Book model with a price field and title field. Now, we want to retrieve the maximum price of all books.
models.py: We have added a min value validator so that the prices of a book can never be less than 0.00.
Python
# models.py
class Book(models.Model):
title = models.CharField(max_length=255)
price = models.DecimalField(max_digits=5,
decimal_places=2,
validators=[MinValueValidator(Decimal('0.01'))])
def __str__(self) -> str:
return f'{self.title}_price_{str(self.price)}'
Let's create some Book objects using the Django shell and perform aggregation.
python manage.py shell
Django Shell:
>>> from myapp.models import Book
>>> Book.objects.create(title='Book 1', price=10.99)
<Book: Book 1_price_10.99>
>>> Book.objects.create(title='Book 2', price=20.99)
<Book: Book 2_price_20.99>
>>> Book.objects.create(title='Book 3', price=30.99)
<Book: Book 3_price_30.99>
>>> Book.objects.create(title='Book 4', price=40.99)
<Book: Book 4_price_40.99>
>>> Book.objects.create(title='Book 5', price=50.99)
<Book: Book 5_price_50.99>
Get the Maximum Price
To get the maximum price, we can use the aggregate() method as follows:
In this example, Book.objects.aggregate(Max('price')) returns a dictionary with a single key-value pair, where the key is price__max and the value is the maximum price. We then access the maximum price using the key price__max.
>>> from django.db.models import Max
>>> Book.objects.aggregate(Max('price'))
{'price__max': Decimal('50.9900000000000')}
# Extract data
>>> Book.objects.aggregate(Max('price'))['price__max']
Decimal('50.9900000000000')
The default key name will always be like <field_name>__<aggreate_function_name>. Also, we can pass the default value in case there are no instances in the database.
Passing the Default value
Let's see how can we change the default name and pass a default value.
>>> Book.objects.aggregate(max_price=Max('price', default=0))
{'max_price': Decimal('50.9900000000000')}
>>> data = Book.objects.aggregate(max_price=Max('price', default=0))
>>> data['max_price']
Decimal('50.9900000000000')
In the above code snippet, we have changed the default name from price__max to max_price.
Method 2 - Using Django order_by()
We can use the order_by() method to find the maximum value without using the Max function.
The SQL equivalent is:
SELECT price FROM book ORDRE BY price DESC LIMIT 1;
Let's find all books ordered by prices in descending order:
>>> Book.objects.all().order_by('-price')
<QuerySet [<Book: Book 5_price_50.99>, <Book: Book 4_price_40.99>, <Book: Book 3_price_30.99>,
<Book: Book 2_price_20.99>, <Book: Book 1_price_10.99>]>
To find the max price, we only need to retrieve the first instance.
>>> Book.objects.all().order_by('-price').first()
<Book: Book 5_price_50.99>
>>>
>>> Book.objects.all().order_by('-price').first().price
Decimal('50.99')
In this example, we use order_by('-price') to order the books by price in descending order, and then use first() to get the first book, which has the maximum price.
In case there is no instance in the database, the query set would be empty and we would need to apply a check.
Find Instance with Max Price
With the aggregate function, we can only find the max_price. To find the Book with the maximum price, we will need to make a few changes. But with the order_by() method, we can easily find the book with the maximum price. As we already did in the above example.
>>> Book.objects.filter(price=Book.objects.aggregate(
max_price=Max('price')
)['max_price'])
<QuerySet [<Book: Book 5_price_50.99>]>
>>> Book.objects.filter(price=Book.objects.aggregate(max_price=Max('price'))['max_price']).first()
<Book: Book 5_price_50.99>
In the above example, we have applied a filter Book having maximum prices and retrieving the first instance.
Conclusion
In this article, we've learned how to get the maximum value of a field in Django using the `aggregate()` and `order_by()` methods. By using the `Max` function, we can easily find the highest value in a model, like the maximum price of a product or the top score in a game.
Similar Reads
How to do a not equal in Django queryset
In Django, filtering data from the database is typically done through QuerySet methods provided by Djangoâs ORM (Object Relational Mapping). When you need to filter records where a certain field is not equal to a specific value, Django offers an elegant way to handle this using the exclude() method
4 min read
How to Use MaxLengthValidator in Django
Django, an excessive degree Python web framework, affords a plethora of gear and capabilities to make net development simpler and extra green. One such feature is the MaxLengthValidator, a validation tool that lets you enforce man or woman limits on entering fields for your Django fashions. In this
3 min read
How to use Regex Validator in Django?
Django, a popular web framework, provides a built-in regex validator that allows you to enforce specific patterns on input data. Whether you need to validate user input, form fields, or any other data in your Django application, regex validators can be incredibly useful. In this article, we'll explo
3 min read
How to create superuser in Django?
Django comes with a built-in admin panel that allows developers to manage the database, users and other models efficiently. This eliminates the need for creating a separate admin interface from scratch. To access and manage the Django Admin panel, we need to create a superuser i.e. a user with full
2 min read
What Does f() do in Django?
In Django, the F() expression is a powerful tool used to perform database operations at the database level. It allows you to reference the value of a model field directly in your query, rather than using Python variables or functions. This can be particularly useful for updating fields based on thei
3 min read
How to get JSON data from request in Django?
Handling incoming JSON data in Django is a common task when building web applications. Whether you're developing an API or a web service, it's crucial to understand how to parse and use JSON data sent in requests. In this article, we will create a simple Django project to demonstrate how to retrieve
2 min read
How to Query as GROUP BY in Django?
In Django, the powerful ORM (Object-Relational Mapping) allows developers to interact with databases using Python code. One common database operation is the GROUP BY query, which groups rows sharing a property so that aggregate functions can be applied to each group. This article will guide you thro
3 min read
How to get GET request values in Django?
Django, a high-level Python web framework, simplifies the process of web development. One common task in web applications is handling GET requests, which are typically used to retrieve data from a server. In this article, we'll create a small Django project that displays a message using data from a
2 min read
How to Rename Items in values() in Django?
In Django, the values() method of a QuerySet is a powerful tool that allows us to return a subset of fields from the database. This is especially useful when we need only specific columns from a model and don't require the entire model instance. A noteworthy feature of the values() method is its abi
3 min read
How to Make Many-to-Many Field Optional in Django
In Django, many-to-many relationships are used when a model can have relationships with multiple instances of another model and vice versa. For example, a book can have many authors, and an author can have written many books. To handle these kinds of relationships, Django provides the ManyToManyFiel
5 min read