mirror of
https://github.com/nprasad2077/nbaStats.git
synced 2026-11-13 21:33:46 +00:00
create view and logic to serve 2P and 3P data
This commit is contained in:
1 parent
0be586a222
commit
08e60d6838
1 file changed
+31
@@ -4,6 +4,9 @@ from django.core import serializers
|
|||||||
from .models import PlayerData
|
from .models import PlayerData
|
||||||
from rest_framework import generics
|
from rest_framework import generics
|
||||||
from .serializers import PlayerDataSerializer
|
from .serializers import PlayerDataSerializer
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from django.db.models import Avg
|
||||||
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
@@ -50,3 +53,31 @@ class TopScorersbySeasonList(generics.ListAPIView):
|
|||||||
season = self.kwargs['season']
|
season = self.kwargs['season']
|
||||||
return PlayerData.objects.filter(season=season).order_by('-PTS')[:20]
|
return PlayerData.objects.filter(season=season).order_by('-PTS')[:20]
|
||||||
|
|
||||||
|
|
||||||
|
# Fetch and calculate the average 3P made, 3P attemps, 2P made, 2P attempts for all players in each season.
|
||||||
|
# Will use APIview here instead of generics.ListAPIView to avoid having to create a serializer to generate the queryset. avg_three_made and avg_two_made are new data points at the time of creation when compared to the per_game model.
|
||||||
|
class ThreeTopPointTrends(APIView):
|
||||||
|
def get(self, request, format=None):
|
||||||
|
seasons = PlayerData.objects.values_list(
|
||||||
|
'season', flat=True).distinct().order_by('season')
|
||||||
|
data = []
|
||||||
|
|
||||||
|
for season in seasons:
|
||||||
|
season_data = PlayerData.objects.filter(season=season)
|
||||||
|
avg_three_made = season_data.aggregate(Avg('three_fg'))[
|
||||||
|
'three_fg_avg']
|
||||||
|
avg_three_attempts = season_data.aggregate(Avg('three_attempts'))[
|
||||||
|
'three_attempts_avg']
|
||||||
|
avg_two_made = season_data.aggregate(Avg('two_fg'))['two_fg_avg']
|
||||||
|
avg_two_attempts = season_data.aggregate(Avg('two_attempts'))[
|
||||||
|
'two_attempts_avg']
|
||||||
|
|
||||||
|
data.append({
|
||||||
|
'season': season,
|
||||||
|
'avg_three_made': avg_three_made,
|
||||||
|
'avg_three_attempts': avg_three_attempts,
|
||||||
|
'avg_two_made': avg_two_made,
|
||||||
|
'avg_two_attempts': avg_two_attempts,
|
||||||
|
})
|
||||||
|
|
||||||
|
return Response(data)
|
||||||
Reference in new issue
Block a user