Запрос
Код: Выделить всё
years = (Transactions.objects
.annotate(year=TruncYear('timestamp'))
.values('year')
.order_by('-year')
.annotate(total=Sum('amount'))
.annotate(earnings=Sum('amount', filter=Q(amount__gt=0)))
.annotate(spent=Sum('amount', filter=Q(amount__lt=0)))
.annotate(count=Count('id'))
.values('year', 'total', 'count', 'earnings', 'spent'))
Подробнее здесь: https://stackoverflow.com/questions/793 ... o-queryset
Мобильная версия