Commit 8d1d9901 by Tom Christie

Pagination docs

parent 1d432cf4
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
REST framework includes a `PaginationSerializer` class that makes it easy to return paginated data in a way that can then be rendered to arbitrary media types. REST framework includes a `PaginationSerializer` class that makes it easy to return paginated data in a way that can then be rendered to arbitrary media types.
## Examples ## Paginating basic data
Let's start by taking a look at an example from the Django documentation. Let's start by taking a look at an example from the Django documentation.
...@@ -35,6 +35,8 @@ The `context` argument of the `PaginationSerializer` class may optionally includ ...@@ -35,6 +35,8 @@ The `context` argument of the `PaginationSerializer` class may optionally includ
We could now return that data in a `Response` object, and it would be rendered into the correct media type. We could now return that data in a `Response` object, and it would be rendered into the correct media type.
## Paginating QuerySets
Our first example worked because we were using primative objects. If we wanted to paginate a queryset or other complex data, we'd need to specify a serializer to use to serialize the result set itself with. Our first example worked because we were using primative objects. If we wanted to paginate a queryset or other complex data, we'd need to specify a serializer to use to serialize the result set itself with.
We can do this using the `object_serializer_class` attribute on the inner `Meta` class of the pagination serializer. For example. We can do this using the `object_serializer_class` attribute on the inner `Meta` class of the pagination serializer. For example.
...@@ -83,16 +85,20 @@ You can also set the pagination style on a per-view basis, using the `ListAPIVie ...@@ -83,16 +85,20 @@ You can also set the pagination style on a per-view basis, using the `ListAPIVie
pagination_serializer_class = CustomPaginationSerializer pagination_serializer_class = CustomPaginationSerializer
paginate_by = 10 paginate_by = 10
For more complex requirements such as serialization that differs depending on the requested media type you can override the `.get_paginate_by()` and `.get_pagination_serializer_class()` methods.
## Creating custom pagination serializers ## Creating custom pagination serializers
Override `pagination.BasePaginationSerializer`, and set the fields that you want the serializer to return. To create a custom pagination serializer class you should override `pagination.BasePaginationSerializer` and set the fields that you want the serializer to return.
For example, to nest a pair of links labelled 'prev' and 'next' you might use something like this.
For example. class LinksSerializer(serializers.Serializer):
next = pagination.NextURLField(source='*')
prev = pagination.PreviousURLField(source='*')
class CustomPaginationSerializer(pagination.BasePaginationSerializer): class CustomPaginationSerializer(pagination.BasePaginationSerializer):
next = pagination.NextURLField() links = LinksSerializer(source='*') # Takes the page object as the source
total_results = serializers.Field(source='paginator.count') total_results = serializers.Field(source='paginator.count')
[cite]: https://docs.djangoproject.com/en/dev/topics/pagination/ [cite]: https://docs.djangoproject.com/en/dev/topics/pagination/
...@@ -85,6 +85,7 @@ The API guide is your complete reference manual to all the functionality provide ...@@ -85,6 +85,7 @@ The API guide is your complete reference manual to all the functionality provide
* [Authentication][authentication] * [Authentication][authentication]
* [Permissions][permissions] * [Permissions][permissions]
* [Throttling][throttling] * [Throttling][throttling]
* [Pagination][pagination]
* [Content negotiation][contentnegotiation] * [Content negotiation][contentnegotiation]
* [Format suffixes][formatsuffixes] * [Format suffixes][formatsuffixes]
* [Returning URLs][reverse] * [Returning URLs][reverse]
...@@ -161,6 +162,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -161,6 +162,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[authentication]: api-guide/authentication.md [authentication]: api-guide/authentication.md
[permissions]: api-guide/permissions.md [permissions]: api-guide/permissions.md
[throttling]: api-guide/throttling.md [throttling]: api-guide/throttling.md
[pagination]: api-guide/pagination.md
[contentnegotiation]: api-guide/content-negotiation.md [contentnegotiation]: api-guide/content-negotiation.md
[formatsuffixes]: api-guide/format-suffixes.md [formatsuffixes]: api-guide/format-suffixes.md
[reverse]: api-guide/reverse.md [reverse]: api-guide/reverse.md
......
...@@ -36,14 +36,14 @@ pre { ...@@ -36,14 +36,14 @@ pre {
} }
/* GitHub 'Star' badge */ /* GitHub 'Star' badge */
body.index #main-content iframe { body.index-page #main-content iframe {
float: right; float: right;
margin-top: -12px; margin-top: -12px;
margin-right: -15px; margin-right: -15px;
} }
/* Travis CI badge */ /* Travis CI badge */
body.index #main-content p:first-of-type { body.index-page #main-content p:first-of-type {
float: right; float: right;
margin-right: 8px; margin-right: 8px;
margin-top: -14px; margin-top: -14px;
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
<!--[if lt IE 9]> <!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]--> <![endif]-->
<body onload="prettyPrint()" class="{{ page_id }}"> <body onload="prettyPrint()" class="{{ page_id }}-page">
<div class="navbar navbar-inverse navbar-fixed-top"> <div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner"> <div class="navbar-inner">
...@@ -55,6 +55,7 @@ ...@@ -55,6 +55,7 @@
<li><a href="{{ base_url }}/api-guide/authentication{{ suffix }}">Authentication</a></li> <li><a href="{{ base_url }}/api-guide/authentication{{ suffix }}">Authentication</a></li>
<li><a href="{{ base_url }}/api-guide/permissions{{ suffix }}">Permissions</a></li> <li><a href="{{ base_url }}/api-guide/permissions{{ suffix }}">Permissions</a></li>
<li><a href="{{ base_url }}/api-guide/throttling{{ suffix }}">Throttling</a></li> <li><a href="{{ base_url }}/api-guide/throttling{{ suffix }}">Throttling</a></li>
<li><a href="{{ base_url }}/api-guide/pagination{{ suffix }}">Pagination</a></li>
<li><a href="{{ base_url }}/api-guide/content-negotiation{{ suffix }}">Content negotiation</a></li> <li><a href="{{ base_url }}/api-guide/content-negotiation{{ suffix }}">Content negotiation</a></li>
<li><a href="{{ base_url }}/api-guide/format-suffixes{{ suffix }}">Format suffixes</a></li> <li><a href="{{ base_url }}/api-guide/format-suffixes{{ suffix }}">Format suffixes</a></li>
<li><a href="{{ base_url }}/api-guide/reverse{{ suffix }}">Returning URLs</a></li> <li><a href="{{ base_url }}/api-guide/reverse{{ suffix }}">Returning URLs</a></li>
...@@ -122,4 +123,4 @@ ...@@ -122,4 +123,4 @@
event.stopPropagation(); event.stopPropagation();
}); });
</script> </script>
</body></html> </body></html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment