diff options
| -rw-r--r-- | docs/api-guide/generic-views.md | 3 | ||||
| -rw-r--r-- | docs/api-guide/settings.md | 6 | ||||
| -rw-r--r-- | rest_framework/mixins.py | 18 | ||||
| -rw-r--r-- | rest_framework/settings.py | 4 | 
4 files changed, 22 insertions, 9 deletions
| diff --git a/docs/api-guide/generic-views.md b/docs/api-guide/generic-views.md index 9e5119cb..734a91e9 100644 --- a/docs/api-guide/generic-views.md +++ b/docs/api-guide/generic-views.md @@ -149,7 +149,8 @@ Should be mixed in with [MultipleObjectAPIView].  **Arguments**: -* `page_size` - Hook to adjust page_size per request. +* `allow_page_size_param` - Allows you to overwrite the global settings `ALLOW_PAGE_SIZE_PARAM` for a specific view. +* `page_size_param` - Allows you to customize the page_size parameter. Default is `page_size`.  ## CreateModelMixin diff --git a/docs/api-guide/settings.md b/docs/api-guide/settings.md index 4f87b30d..2f90369b 100644 --- a/docs/api-guide/settings.md +++ b/docs/api-guide/settings.md @@ -150,4 +150,10 @@ Default: `'accept'`  Default: `'format'` +## ALLOW_PAGE_SIZE_PARAM + +Allows you to globally pass a page size parameter for an individual request. + +Default: `'True'` +  [cite]: http://www.python.org/dev/peps/pep-0020/ diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index f725fc9e..d64e7e56 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.  from django.http import Http404  from rest_framework import status  from rest_framework.response import Response +from rest_framework.settings import api_settings  class CreateModelMixin(object): @@ -32,6 +33,8 @@ class ListModelMixin(object):      Should be mixed in with `MultipleObjectAPIView`.      """      empty_error = u"Empty list and '%(class_name)s.allow_empty' is False." +    allow_page_size_param = api_settings.ALLOW_PAGE_SIZE_PARAM +    page_size_param = 'page_size'      def list(self, request, *args, **kwargs):          self.object_list = self.get_filtered_queryset() @@ -56,13 +59,14 @@ class ListModelMixin(object):          return Response(serializer.data)      def get_paginate_by(self, queryset): -        page_size_param = self.request.QUERY_PARAMS.get('page_size') -        if page_size_param: -            try: -                page_size = int(page_size_param) -                return page_size -            except ValueError: -                pass +        if self.allow_page_size_param: +            page_size_param = self.request.QUERY_PARAMS.get(self.page_size_param) +            if page_size_param: +                try: +                    page_size = int(page_size_param) +                    return page_size +                except ValueError: +                    pass          return super(ListModelMixin, self).get_paginate_by(queryset) diff --git a/rest_framework/settings.py b/rest_framework/settings.py index 906a7cf6..1daa9dfd 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -66,7 +66,9 @@ DEFAULTS = {      'URL_ACCEPT_OVERRIDE': 'accept',      'URL_FORMAT_OVERRIDE': 'format', -    'FORMAT_SUFFIX_KWARG': 'format' +    'FORMAT_SUFFIX_KWARG': 'format', + +    'ALLOW_PAGE_SIZE_PARAM': True  } | 
