Skip to content

Instantly share code, notes, and snippets.

@susmithagudapati
Created April 29, 2020 19:10
Show Gist options
  • Save susmithagudapati/583fbd219d62a06f1a0dd1b8d8fd3e58 to your computer and use it in GitHub Desktop.
Save susmithagudapati/583fbd219d62a06f1a0dd1b8d8fd3e58 to your computer and use it in GitHub Desktop.
# usage of annotation
# annotates number of attributes to each product.
products = Product.objects.annotate(number_of_attributes=Count('attributes'))
products[0].number_of_attributes
4
# the other way to define
products = Product.objects.annotate(Count('attributes'))
products[0].attributes__count
4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment