2

So I have a query like

SELECT sum(project_shares) as shares, count(*) as count FROM vv_projects

Is there any syntax like below in django 2.0

Projects.objects.aggregrate(Sum('project_shares'),Count('*'))

and output like

{'project_shares_sum':9,'count':8}

In django 2.0 count is used aggregate foreign key references so I am confused. If not I have add another orm query line to get the count.

Sayok88
  • 2,038
  • 1
  • 14
  • 22

1 Answers1

2

The syntax you provided should work. All you have to do is to replace Count('*') with Count('id') or Count('pk') to count all the entries.

Projects.objects.aggregrate(Sum('project_shares'), Count('pk'))

Output will be like:

{'project_shares__sum':9,'pk__count':8}
Peter Sobhi
  • 2,542
  • 2
  • 22
  • 28