diff --git a/unisportomat/quiz/pagination.py b/unisportomat/quiz/pagination.py index 385a0610f6095cde89299ff5e4ebd25251083f6b..058ca4b7412d854a8e1e25e17020e2a2f03d0983 100644 --- a/unisportomat/quiz/pagination.py +++ b/unisportomat/quiz/pagination.py @@ -5,6 +5,7 @@ or be given globally via settings.py. """ from rest_framework.pagination import PageNumberPagination +from rest_framework.response import Response class PageNumberWithPageSizePagination(PageNumberPagination): diff --git a/unisportomat/quiz/views.py b/unisportomat/quiz/views.py index 7207d88ef558aabcd8c067abd98f2eb689da85b3..275d97ad148bff07106c802555b0dc591aec5dfb 100644 --- a/unisportomat/quiz/views.py +++ b/unisportomat/quiz/views.py @@ -7,6 +7,8 @@ from rest_framework import viewsets from rest_framework.views import APIView from rest_framework.response import Response from django.http import JsonResponse +from .pagination import PageNumberWithPageSizePagination + from .serializers import ( SmallSportListSerializer, SportListSerializer, @@ -63,11 +65,30 @@ class SmallSportListView(viewsets.ViewSet): # GET for api/admin/sport/ def list(self, request): + paginator = PageNumberWithPageSizePagination() + """ + Comments on Pagination: + Every list that needs to be paginated needs a paginator. + Here, the Paginator object is created in the GET call. + The Queryset which is supposed to be paginated needs to be run through the function: + + > new_queryset = paginator.paginate_queryset(complete_queryset, request) + + The new_queryset is a List, not a Manager, so it can be directly iterated upon. + After the data has been worked on and run through the Serializer as normal, + instead of returning Result(data), the paginator needs to be used again so it can add its page metadata: + + > return paginator.get_paginated_response(serializer.data) + + This function already returns a fully valid Response, so it can be directly returned. + """ + sports = Sport.objects.all() + sports = paginator.paginate_queryset(sports, request) criteria = Criterion.objects.all() is_filled_tuples = [] - for sport in sports.iterator(): + for sport in sports: filled_criteria_pks = [] @@ -88,7 +109,7 @@ class SmallSportListView(viewsets.ViewSet): serializer = SmallSportListSerializer(is_filled_tuples) - return Response(serializer.data) + return paginator.get_paginated_response(serializer.data) # POST for api/admin/sport/ def create(self, request):