Expand pagination support, add docs

This commit is contained in:
Tom Christie 2012-10-01 15:49:19 +01:00
parent 6fa589fefd
commit b16fb57771
7 changed files with 223 additions and 26 deletions

View File

@ -0,0 +1,98 @@
<a class="github" href="pagination.py"></a>
# Pagination
> Django provides a few classes that help you manage paginated data that is, data thats split across several pages, with “Previous/Next” links.
>
> &mdash; [Django documentation][cite]
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
Let's start by taking a look at an example from the Django documentation.
from django.core.paginator import Paginator
objects = ['john', 'paul', 'george', 'ringo']
paginator = Paginator(objects, 2)
page = paginator.page(1)
page.object_list
# ['john', 'paul']
At this point we've got a page object. If we wanted to return this page object as a JSON response, we'd need to provide the client with context such as next and previous links, so that it would be able to page through the remaining results.
from rest_framework.pagination import PaginationSerializer
serializer = PaginationSerializer(instance=page)
serializer.data
# {'count': 4, 'next': '?page=2', 'previous': None, 'results': [u'john', u'paul']}
The `context` argument of the `PaginationSerializer` class may optionally include the request. If the request is included in the context then the next and previous links returned by the serializer will use absolute URLs instead of relative URLs.
request = RequestFactory().get('/foobar')
serializer = PaginationSerializer(instance=page, context={'request': request})
serializer.data
# {'count': 4, 'next': 'http://testserver/foobar?page=2', 'previous': None, 'results': [u'john', u'paul']}
We could now return that data in a `Response` object, and it would be rendered into the correct media type.
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.
class UserSerializer(serializers.ModelSerializer):
"""
Serializes user querysets.
"""
class Meta:
model = User
fields = ('username', 'email')
class PaginatedUserSerializer(pagination.PaginationSerializer):
"""
Serializes page objects of user querysets.
"""
class Meta:
object_serializer_class = UserSerializer
queryset = User.objects.all()
paginator = Paginator(queryset, 20)
page = paginator.page(1)
serializer = PaginatedUserSerializer(instance=page)
serializer.data
# {'count': 1, 'next': None, 'previous': None, 'results': [{'username': u'admin', 'email': u'admin@example.com'}]}
## Pagination in the generic views
The generic class based views `ListAPIView` and `ListCreateAPIView` provide pagination of the returned querysets by default. You can customise this behaviour by altering the pagination style, by modifying the default number of results, or by turning pagination off completely.
## Setting the default pagination style
The default pagination style may be set globally, using the `PAGINATION_SERIALIZER` and `PAGINATE_BY` settings. For example.
REST_FRAMEWORK = {
'PAGINATION_SERIALIZER': (
'example_app.pagination.CustomPaginationSerializer',
),
'PAGINATE_BY': 10
}
You can also set the pagination style on a per-view basis, using the `ListAPIView` generic class-based view.
class PaginatedListView(ListAPIView):
model = ExampleModel
pagination_serializer_class = CustomPaginationSerializer
paginate_by = 10
## Creating custom pagination serializers
Override `pagination.BasePaginationSerializer`, and set the fields that you want the serializer to return.
For example.
class CustomPaginationSerializer(pagination.BasePaginationSerializer):
next = pagination.NextURLField()
total_results = serializers.Field(source='paginator.count')
[cite]: https://docs.djangoproject.com/en/dev/topics/pagination/

View File

@ -158,6 +158,8 @@ class Field(object):
return value
elif hasattr(self, 'model_field'):
return self.model_field.value_to_string(self.obj)
elif hasattr(value, '__iter__') and not isinstance(value, (dict, basestring)):
return [self.to_native(item) for item in value]
return smart_unicode(value)
def attributes(self):

View File

@ -16,20 +16,24 @@ class BaseView(views.APIView):
"""
serializer_class = None
model_serializer_class = api_settings.MODEL_SERIALIZER
pagination_serializer_class = api_settings.PAGINATION_SERIALIZER
paginate_by = api_settings.PAGINATE_BY
def get_serializer_context(self):
"""
Extra context provided to the serializer class.
"""
return {
'request': self.request,
'format': self.kwargs.get('format', None)
'format': self.format,
'view': self
}
def get_serializer(self, data=None, files=None, instance=None, kwargs=None):
# TODO: add support for files
# TODO: add support for seperate serializer/deserializer
def get_serializer_class(self):
"""
Return the class to use for the serializer.
Use `self.serializer_class`, falling back to constructing a
model serializer class from `self.model_serializer_class`
"""
serializer_class = self.serializer_class
kwargs = kwargs or {}
if serializer_class is None:
class DefaultSerializer(self.model_serializer_class):
@ -37,22 +41,38 @@ class BaseView(views.APIView):
model = self.model
serializer_class = DefaultSerializer
context = self.get_serializer_context()
return serializer_class(data, instance=instance, context=context, **kwargs)
return serializer_class
def get_pagination_serializer(self, page=None):
serializer_class = self.pagination_serializer_class
def get_serializer(self, data=None, files=None, instance=None):
# TODO: add support for files
# TODO: add support for seperate serializer/deserializer
serializer_class = self.get_serializer_class()
context = self.get_serializer_context()
ret = serializer_class(instance=page, context=context)
ret.fields['results'] = self.get_serializer(kwargs={'source': 'object_list'})
return ret
return serializer_class(data, instance=instance, context=context)
class MultipleObjectBaseView(MultipleObjectMixin, BaseView):
"""
Base class for generic views onto a queryset.
"""
pass
pagination_serializer_class = api_settings.PAGINATION_SERIALIZER
paginate_by = api_settings.PAGINATE_BY
def get_pagination_serializer_class(self):
"""
Return the class to use for the pagination serializer.
"""
class SerializerClass(self.pagination_serializer_class):
class Meta:
object_serializer_class = self.get_serializer_class()
return SerializerClass
def get_pagination_serializer(self, page=None):
pagination_serializer_class = self.get_pagination_serializer_class()
context = self.get_serializer_context()
return pagination_serializer_class(instance=page, context=context)
class SingleObjectBaseView(SingleObjectMixin, BaseView):

View File

@ -4,27 +4,64 @@ from rest_framework import serializers
class NextPageField(serializers.Field):
"""
Field that returns a link to the next page in paginated results.
"""
def to_native(self, value):
if not value.has_next():
return None
page = value.next_page_number()
request = self.context['request']
return request.build_absolute_uri('?page=%d' % page)
request = self.context.get('request')
relative_url = '?page=%d' % page
if request:
return request.build_absolute_uri(relative_url)
return relative_url
class PreviousPageField(serializers.Field):
"""
Field that returns a link to the previous page in paginated results.
"""
def to_native(self, value):
if not value.has_previous():
return None
page = value.previous_page_number()
request = self.context['request']
return request.build_absolute_uri('?page=%d' % page)
request = self.context.get('request')
relative_url = '?page=%d' % page
if request:
return request.build_absolute_uri('?page=%d' % page)
return relative_url
class PaginationSerializer(serializers.Serializer):
count = serializers.Field(source='paginator.count')
next = NextPageField(source='*')
previous = PreviousPageField(source='*')
class PaginationSerializerOptions(serializers.SerializerOptions):
"""
An object that stores the options that may be provided to a
pagination serializer by using the inner `Meta` class.
Accessible on the instance as `serializer.opts`.
"""
def __init__(self, meta):
super(PaginationSerializerOptions, self).__init__(meta)
self.object_serializer_class = getattr(meta, 'object_serializer_class',
serializers.Field)
class BasePaginationSerializer(serializers.Serializer):
"""
A base class for pagination serializers to inherit from,
to make implementing custom serializers more easy.
"""
_options_class = PaginationSerializerOptions
_results_field = 'results'
def __init__(self, *args, **kwargs):
"""
Override init to add in the object serializer field on-the-fly.
"""
super(BasePaginationSerializer, self).__init__(*args, **kwargs)
results_field = self._results_field
object_serializer = self.opts.object_serializer_class
self.fields[results_field] = object_serializer(source='object_list')
def to_native(self, obj):
"""
@ -32,3 +69,12 @@ class PaginationSerializer(serializers.Serializer):
each in turn.
"""
return self.convert_object(obj)
class PaginationSerializer(BasePaginationSerializer):
"""
A default implementation of a pagination serializer.
"""
count = serializers.Field(source='paginator.count')
next = NextPageField(source='*')
previous = PreviousPageField(source='*')

View File

@ -70,7 +70,7 @@ class SerializerMetaclass(type):
class SerializerOptions(object):
"""
Meta class options for ModelSerializer
Meta class options for Serializer
"""
def __init__(self, meta):
self.nested = getattr(meta, 'nested', False)

View File

@ -13,6 +13,7 @@ class RootView(generics.RootAPIView):
Example description for OPTIONS.
"""
model = BasicModel
paginate_by = None
class InstanceView(generics.InstanceAPIView):

View File

@ -1,6 +1,7 @@
from django.core.paginator import Paginator
from django.test import TestCase
from django.test.client import RequestFactory
from rest_framework import generics, status
from rest_framework import generics, status, pagination
from rest_framework.tests.models import BasicModel
factory = RequestFactory()
@ -14,7 +15,11 @@ class RootView(generics.RootAPIView):
paginate_by = 10
class TestPaginatedView(TestCase):
class IntegrationTestPagination(TestCase):
"""
Integration tests for paginated list views.
"""
def setUp(self):
"""
Create 26 BasicModel intances.
@ -55,3 +60,28 @@ class TestPaginatedView(TestCase):
self.assertEquals(response.data['results'], self.data[20:])
self.assertEquals(response.data['next'], None)
self.assertNotEquals(response.data['previous'], None)
class UnitTestPagination(TestCase):
"""
Unit tests for pagination of primative objects.
"""
def setUp(self):
self.objects = [char * 3 for char in 'abcdefghijklmnopqrstuvwxyz']
paginator = Paginator(self.objects, 10)
self.first_page = paginator.page(1)
self.last_page = paginator.page(3)
def test_native_pagination(self):
serializer = pagination.PaginationSerializer(instance=self.first_page)
self.assertEquals(serializer.data['count'], 26)
self.assertEquals(serializer.data['next'], '?page=2')
self.assertEquals(serializer.data['previous'], None)
self.assertEquals(serializer.data['results'], self.objects[:10])
serializer = pagination.PaginationSerializer(instance=self.last_page)
self.assertEquals(serializer.data['count'], 26)
self.assertEquals(serializer.data['next'], None)
self.assertEquals(serializer.data['previous'], '?page=2')
self.assertEquals(serializer.data['results'], self.objects[20:])