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

Add Pagination to small-sport-list

parent 59927cd3
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ or be given globally via settings.py. ...@@ -5,6 +5,7 @@ or be given globally via settings.py.
""" """
from rest_framework.pagination import PageNumberPagination from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
class PageNumberWithPageSizePagination(PageNumberPagination): class PageNumberWithPageSizePagination(PageNumberPagination):
......
...@@ -7,6 +7,8 @@ from rest_framework import viewsets ...@@ -7,6 +7,8 @@ from rest_framework import viewsets
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from django.http import JsonResponse from django.http import JsonResponse
from .pagination import PageNumberWithPageSizePagination
from .serializers import ( from .serializers import (
SmallSportListSerializer, SmallSportListSerializer,
SportListSerializer, SportListSerializer,
...@@ -63,11 +65,30 @@ class SmallSportListView(viewsets.ViewSet): ...@@ -63,11 +65,30 @@ class SmallSportListView(viewsets.ViewSet):
# GET for api/admin/sport/ # GET for api/admin/sport/
def list(self, request): 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 = Sport.objects.all()
sports = paginator.paginate_queryset(sports, request)
criteria = Criterion.objects.all() criteria = Criterion.objects.all()
is_filled_tuples = [] is_filled_tuples = []
for sport in sports.iterator(): for sport in sports:
filled_criteria_pks = [] filled_criteria_pks = []
...@@ -88,7 +109,7 @@ class SmallSportListView(viewsets.ViewSet): ...@@ -88,7 +109,7 @@ class SmallSportListView(viewsets.ViewSet):
serializer = SmallSportListSerializer(is_filled_tuples) serializer = SmallSportListSerializer(is_filled_tuples)
return Response(serializer.data) return paginator.get_paginated_response(serializer.data)
# POST for api/admin/sport/ # POST for api/admin/sport/
def create(self, request): 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