diff options
| author | Tom Christie | 2012-09-30 17:31:28 +0100 | 
|---|---|---|
| committer | Tom Christie | 2012-09-30 17:31:28 +0100 | 
| commit | 6fa589fefd48d98e4f0a11548b6c3e5ced58e31e (patch) | |
| tree | a5d1dd75ce6d5c5be7bf81b386a29c13235d33ff /rest_framework/mixins.py | |
| parent | 43d3634e892e303ca377265d3176e8313f19563f (diff) | |
| download | django-rest-framework-6fa589fefd48d98e4f0a11548b6c3e5ced58e31e.tar.bz2 | |
Pagination support
Diffstat (limited to 'rest_framework/mixins.py')
| -rw-r--r-- | rest_framework/mixins.py | 21 | 
1 files changed, 20 insertions, 1 deletions
diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index fe12dc8f..167cd89a 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -7,6 +7,7 @@ which allows mixin classes to be composed in interesting ways.  Eg. Use mixins to build a Resource class, and have a Router class      perform the binding of http methods to actions for us.  """ +from django.http import Http404  from rest_framework import status  from rest_framework.response import Response @@ -30,9 +31,27 @@ class ListModelMixin(object):      List a queryset.      Should be mixed in with `MultipleObjectBaseView`.      """ +    empty_error = u"Empty list and '%(class_name)s.allow_empty' is False." +      def list(self, request, *args, **kwargs):          self.object_list = self.get_queryset() -        serializer = self.get_serializer(instance=self.object_list) + +        # Default is to allow empty querysets.  This can be altered by setting +        # `.allow_empty = False`, to raise 404 errors on empty querysets. +        allow_empty = self.get_allow_empty() +        if not allow_empty and len(self.object_list) == 0: +            error_args = {'class_name': self.__class__.__name__} +            raise Http404(self.empty_error % error_args) + +        # Pagination size is set by the `.paginate_by` attribute, +        # which may be `None` to disable pagination. +        page_size = self.get_paginate_by(self.object_list) +        if page_size: +            paginator, page, queryset, is_paginated = self.paginate_queryset(self.object_list, page_size) +            serializer = self.get_pagination_serializer(page) +        else: +            serializer = self.get_serializer(instance=self.object_list) +          return Response(serializer.data)  | 
