Django Model Aggregations Average, Minimum, Maximum, Sum from Queryset


class Product(models.Model):
    name = models.CharField(max_length=20)
    price = models.FloatField()

To Get average price of all products:

>>> from django.db.models import Avg, Max, Min, Sum
>>> Product.objects.all().aggregate(Avg('price'))
# {'price__avg': 124.0}

To Get Minimum price of all products:

>>> Product.objects.all().aggregate(Min('price'))
# {'price__min': 9}

To Get Maximum price of all products:

>>> Product.objects.all().aggregate(Max('price'))
# {'price__max':599 }

To Get SUM of prices of all products:

>>> Product.objects.all().aggregate(Sum('price'))
# {'price__sum':92456 }