Skip to content
Snippets Groups Projects
Commit 8089a2fd authored by borzechof99's avatar borzechof99 :whale2:
Browse files

Add Pagination to small-sport-list

parent 663066e1
No related branches found
No related tags found
No related merge requests found
......@@ -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):
......
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment