Reputation: 674
I have the following snippet:
sum_games = GameHistory.objects.all().aggregate(Sum('games'))
sum_played = GameHistory.objects.all().aggregate(Sum('games_played'))
sum_goals = GameHistory.objects.all().aggregate(Sum('goals'))
sum_assists = GameHistory.objects.all().aggregate(Sum('assists'))
sum_clean_sheet = GameHistory.objects.all().aggregate(Sum('clean_sheet'))
What is the best and cleanest way to write this? Because right now I am just repeating myself but with different variables.
Thanks in advance.
Upvotes: 0
Views: 67
Reputation: 7049
You can separate them with commas.
stats = GameHistory.objects.all().aggregate(sum_games=Sum('games'), sum_games_played=Sum('games_played'), ...)
and then access like so:
print(stats['sum_games'])
print(stats['sum_games_played'])
https://docs.djangoproject.com/en/2.2/topics/db/aggregation/#joins-and-aggregates
Upvotes: 3