Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Backend
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
swp-unisport
team-warumkeinrust
Backend
Commits
8089a2fd
Commit
8089a2fd
authored
3 years ago
by
borzechof99
Browse files
Options
Downloads
Patches
Plain Diff
Add Pagination to small-sport-list
parent
663066e1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
unisportomat/quiz/pagination.py
+1
-0
1 addition, 0 deletions
unisportomat/quiz/pagination.py
unisportomat/quiz/views.py
+23
-2
23 additions, 2 deletions
unisportomat/quiz/views.py
with
24 additions
and
2 deletions
unisportomat/quiz/pagination.py
+
1
−
0
View file @
8089a2fd
...
...
@@ -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
):
...
...
This diff is collapsed.
Click to expand it.
unisportomat/quiz/views.py
+
23
−
2
View file @
8089a2fd
...
...
@@ -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
R
esponse
(
serializer
.
data
)
return
paginator
.
get_paginated_r
esponse
(
serializer
.
data
)
# POST for api/admin/sport/
def
create
(
self
,
request
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment