Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
1bcec3a0
Commit
1bcec3a0
authored
Jan 13, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
API tweaks and pagination documentation
parent
564f845e
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
12 deletions
+22
-12
docs/api-guide/pagination.md
+0
-0
rest_framework/generics.py
+3
-3
rest_framework/pagination.py
+19
-9
No files found.
docs/api-guide/pagination.md
View file @
1bcec3a0
This diff is collapsed.
Click to expand it.
rest_framework/generics.py
View file @
1bcec3a0
...
...
@@ -160,11 +160,11 @@ class GenericAPIView(views.APIView):
def
paginate_queryset
(
self
,
queryset
):
if
self
.
pager
is
None
:
return
None
return
queryset
return
self
.
pager
.
paginate_queryset
(
queryset
,
self
.
request
,
view
=
self
)
def
get_paginated_response
(
self
,
objects
):
return
self
.
pager
.
get_paginated_response
(
objects
)
def
get_paginated_response
(
self
,
data
):
return
self
.
pager
.
get_paginated_response
(
data
)
# Concrete view classes that provide method handlers
...
...
rest_framework/pagination.py
View file @
1bcec3a0
...
...
@@ -25,11 +25,21 @@ def _strict_positive_int(integer_string, cutoff=None):
return
ret
def
_get_count
(
queryset
):
"""
Determine an object count, supporting either querysets or regular lists.
"""
try
:
return
queryset
.
count
()
except
AttributeError
:
return
len
(
queryset
)
class
BasePagination
(
object
):
def
paginate_queryset
(
self
,
queryset
,
request
):
def
paginate_queryset
(
self
,
queryset
,
request
,
view
):
raise
NotImplemented
(
'paginate_queryset() must be implemented.'
)
def
get_paginated_response
(
self
,
data
,
page
,
request
):
def
get_paginated_response
(
self
,
data
):
raise
NotImplemented
(
'get_paginated_response() must be implemented.'
)
...
...
@@ -58,8 +68,8 @@ class PageNumberPagination(BasePagination):
def
paginate_queryset
(
self
,
queryset
,
request
,
view
):
"""
Paginate a queryset if required, either returning a
page object,
or `None` if pagination is not configured for this view.
Paginate a queryset if required, either returning a
page object,
or `None` if pagination is not configured for this view.
"""
for
attr
in
(
'paginate_by'
,
'page_query_param'
,
...
...
@@ -97,12 +107,12 @@ class PageNumberPagination(BasePagination):
self
.
request
=
request
return
self
.
page
def
get_paginated_response
(
self
,
objects
):
def
get_paginated_response
(
self
,
data
):
return
Response
(
OrderedDict
([
(
'count'
,
self
.
page
.
paginator
.
count
),
(
'next'
,
self
.
get_next_link
()),
(
'previous'
,
self
.
get_previous_link
()),
(
'results'
,
objects
)
(
'results'
,
data
)
]))
def
get_page_size
(
self
,
request
):
...
...
@@ -147,16 +157,16 @@ class LimitOffsetPagination(BasePagination):
def
paginate_queryset
(
self
,
queryset
,
request
,
view
):
self
.
limit
=
self
.
get_limit
(
request
)
self
.
offset
=
self
.
get_offset
(
request
)
self
.
count
=
queryset
.
count
(
)
self
.
count
=
_get_count
(
queryset
)
self
.
request
=
request
return
queryset
[
self
.
offset
:
self
.
offset
+
self
.
limit
]
def
get_paginated_response
(
self
,
objects
):
def
get_paginated_response
(
self
,
data
):
return
Response
(
OrderedDict
([
(
'count'
,
self
.
count
),
(
'next'
,
self
.
get_next_link
()),
(
'previous'
,
self
.
get_previous_link
()),
(
'results'
,
objects
)
(
'results'
,
data
)
]))
def
get_limit
(
self
,
request
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment