Skip to content

Instantly share code, notes, and snippets.

@shepelevstas
Last active July 19, 2022 12:57
Show Gist options
  • Save shepelevstas/251f8db9a249e445868e5c7392e2c431 to your computer and use it in GitHub Desktop.
Save shepelevstas/251f8db9a249e445868e5c7392e2c431 to your computer and use it in GitHub Desktop.
Django
from django.db.models import Sum
from .models import Item
# for django < 1.8
group_sums = Item.objects.filter(deleted=None).values('user').annotate(sum=Sum('price', field='price*quantity'))
# group_sums :: [{'user': 123, 'sum': 9999}]
# values + annotate => GROUP BY in sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment