Aggregation

Aggregation: Aggregation is used to calculate summary statistics on a set of database records. It groups records based on a specified field and then performs an aggregate function (e.g., SUM, COUNT, AVG, etc.) on those grouped records.

Here's a simple example of aggregation in Django using a model named Order with a field total_price:

from django.db.models import Sum
from myapp.models import Order

# Calculate the total price of all orders
total_price = Order.objects.aggregate(total_price=Sum('total_price'))
print(total_price)

Last updated