Skip to content

Instantly share code, notes, and snippets.

@susmithagudapati
Created April 29, 2020 19:09
Show Gist options
  • Save susmithagudapati/dc9623af026729453a5b4a7735aa0f0c to your computer and use it in GitHub Desktop.
Save susmithagudapati/dc9623af026729453a5b4a7735aa0f0c to your computer and use it in GitHub Desktop.
# basic usage of aggregation
# Max price of all products.
from django.db.models import Max
Product.objects.all().aggregate(Max('price'))
{'price__max': 70.15}
# Average view count across all products.
from django.db.models import Avg
Product.objects.all().aggregate(Avg('view_count'))
{'view_count__avg': 10}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment