Skip to content

Instantly share code, notes, and snippets.

@susmithagudapati
Created April 29, 2020 19:46
Show Gist options
  • Save susmithagudapati/210f112d084287d8dc6290aedd8988b4 to your computer and use it in GitHub Desktop.
Save susmithagudapati/210f112d084287d8dc6290aedd8988b4 to your computer and use it in GitHub Desktop.
from django.db.models import Avg, Count
# first annotates each product with number of attributes,
# and then aggregates the average number of authors using the annotated attribute
Product.objects.annotate(num_attributes=Count('attributes')).aggregate(Avg('num_attributes'))
{'num_authors__avg': 3.46}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment