Django rest framework override page_size in ViewSet





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







11















I am having problem with django rest framework pagination.
I have set pagination in settings like -



'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 1


Below is my viewset.



class HobbyCategoryViewSet(viewsets.ModelViewSet):    
serializer_class = HobbyCategorySerializer
queryset = UserHobbyCategory.objects.all()


I want to set different page size for this viewset. I have tried setting page_size and Paginate_by class variables but list is paginated according to PAGE_SIZE defined in settings. Any idea where I am wrong ?










share|improve this question























  • have you tried to set pagination_class?

    – ilse2005
    Feb 16 '16 at 12:48











  • if you want to set a hard maximum limit look at overriding the get_paginate_by method on the generic views.

    – Mushahid Khan
    Feb 16 '16 at 12:51













  • pagination_class worked but I had to create custom pagination class.

    – Mangesh
    Feb 16 '16 at 13:05


















11















I am having problem with django rest framework pagination.
I have set pagination in settings like -



'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 1


Below is my viewset.



class HobbyCategoryViewSet(viewsets.ModelViewSet):    
serializer_class = HobbyCategorySerializer
queryset = UserHobbyCategory.objects.all()


I want to set different page size for this viewset. I have tried setting page_size and Paginate_by class variables but list is paginated according to PAGE_SIZE defined in settings. Any idea where I am wrong ?










share|improve this question























  • have you tried to set pagination_class?

    – ilse2005
    Feb 16 '16 at 12:48











  • if you want to set a hard maximum limit look at overriding the get_paginate_by method on the generic views.

    – Mushahid Khan
    Feb 16 '16 at 12:51













  • pagination_class worked but I had to create custom pagination class.

    – Mangesh
    Feb 16 '16 at 13:05














11












11








11








I am having problem with django rest framework pagination.
I have set pagination in settings like -



'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 1


Below is my viewset.



class HobbyCategoryViewSet(viewsets.ModelViewSet):    
serializer_class = HobbyCategorySerializer
queryset = UserHobbyCategory.objects.all()


I want to set different page size for this viewset. I have tried setting page_size and Paginate_by class variables but list is paginated according to PAGE_SIZE defined in settings. Any idea where I am wrong ?










share|improve this question














I am having problem with django rest framework pagination.
I have set pagination in settings like -



'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 1


Below is my viewset.



class HobbyCategoryViewSet(viewsets.ModelViewSet):    
serializer_class = HobbyCategorySerializer
queryset = UserHobbyCategory.objects.all()


I want to set different page size for this viewset. I have tried setting page_size and Paginate_by class variables but list is paginated according to PAGE_SIZE defined in settings. Any idea where I am wrong ?







python django django-rest-framework






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 16 '16 at 12:44









MangeshMangesh

255213




255213













  • have you tried to set pagination_class?

    – ilse2005
    Feb 16 '16 at 12:48











  • if you want to set a hard maximum limit look at overriding the get_paginate_by method on the generic views.

    – Mushahid Khan
    Feb 16 '16 at 12:51













  • pagination_class worked but I had to create custom pagination class.

    – Mangesh
    Feb 16 '16 at 13:05



















  • have you tried to set pagination_class?

    – ilse2005
    Feb 16 '16 at 12:48











  • if you want to set a hard maximum limit look at overriding the get_paginate_by method on the generic views.

    – Mushahid Khan
    Feb 16 '16 at 12:51













  • pagination_class worked but I had to create custom pagination class.

    – Mangesh
    Feb 16 '16 at 13:05

















have you tried to set pagination_class?

– ilse2005
Feb 16 '16 at 12:48





have you tried to set pagination_class?

– ilse2005
Feb 16 '16 at 12:48













if you want to set a hard maximum limit look at overriding the get_paginate_by method on the generic views.

– Mushahid Khan
Feb 16 '16 at 12:51







if you want to set a hard maximum limit look at overriding the get_paginate_by method on the generic views.

– Mushahid Khan
Feb 16 '16 at 12:51















pagination_class worked but I had to create custom pagination class.

– Mangesh
Feb 16 '16 at 13:05





pagination_class worked but I had to create custom pagination class.

– Mangesh
Feb 16 '16 at 13:05












4 Answers
4






active

oldest

votes


















13














I fixed this by creating custom pagination class. and setting desired pagesize in class. I have used this class as pagination_class in my viewset.



from rest_framework import pagination

class ExamplePagination(pagination.PageNumberPagination):
page_size = 2

class HobbyCategoryViewSet(viewsets.ModelViewSet):
serializer_class = HobbyCategorySerializer
queryset = UserHobbyCategory.objects.all()
pagination_class=ExamplePagination


I am not sure if there is any easier way for this. this one worked for me. But I think its not good to create new class just to change page_size.



Edit - simple solution is set it like



pagination.PageNumberPagination.page_size = 100 


in ViewSet.



class HobbyCategoryViewSet(viewsets.ModelViewSet):    
serializer_class = HobbyCategorySerializer
queryset = UserHobbyCategory.objects.all()
pagination.PageNumberPagination.page_size = 100





share|improve this answer


























  • changing pagination.PageNumberPagination.page_size in different viewsets does not result in different sizes. Only one page_size is set

    – object recognition
    Aug 4 '16 at 7:27



















9














Use page size query params to provide page size dynamically..



from rest_framework.pagination import PageNumberPagination

class StandardResultsSetPagination(PageNumberPagination):
page_size_query_param = 'limit'


Set Default pagination class in settings



REST_FRAMEWORK = {'DEFAULT_PAGINATION_CLASS': 'StandardResultsSetPagination',}


Now in your URL provide limit as a GET parameter..




http://example.com/list/?limit=100 or 25







share|improve this answer


























  • Where do you put the 'StandardResultsSetPagination' class so that 'DEFAULT_PAGINATION_CLASS' can see it ?

    – Curtwagner1984
    Aug 1 '16 at 14:29











  • Edited answer...u need to add in settings

    – Naresh
    Aug 2 '16 at 13:52











  • Yes I know, I meant where do you put the class itself so that the setting DEFAULT_PAGINATION_CLASS in settings.py will see it?

    – Curtwagner1984
    Aug 2 '16 at 14:08











  • @Curtwagner1984 you can put anywhere in your project and refer class with package and module name....i would suggest you create some folder for such generic classes..

    – Naresh
    Aug 3 '16 at 9:45



















1














In case you are trying to change the "page size" of the LimitOffsetPagination class, you have to override the default_limit variable instead of page_size:



from rest_framework import paginationclass 


CustomLimitOffsetPagination(pagination.LimitOffsetPagination):
default_limit = 5





share|improve this answer































    1














    In the case that you want to set a default pagination value, including a max and a param, here is what you do.



    1) Create a drf_defaults.py (or any name you choose). I placed it in same dir as settings.py:



    """
    Django rest framework default pagination
    """
    from rest_framework.pagination import PageNumberPagination


    class DefaultResultsSetPagination(PageNumberPagination):
    page_size = 50
    page_size_query_param = 'page_size'
    max_page_size = 100000


    2) In your settings.py, update REST_FRAMEWORK dict, adding the following:



    'DEFAULT_PAGINATION_CLASS': 'drf_defaults.DefaultResultsSetPagination',


    In the end my REST_FRAMEWORK settings dict looks like:



    # http://www.django-rest-framework.org/api-guide/settings/
    REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
    # 'rest_framework.permissions.AllowAny', # Use to disable api auth
    # 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
    'permissions.IsAuthenticatedWriteOrReadOnly',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': [
    # 'oauth2_provider.contrib.rest_framework.OAuth2Authentication', # Own oauth server
    'client_authentication.ApiTokenAuthentication',
    'rest_framework.authentication.SessionAuthentication',
    'rest_framework.authentication.BasicAuthentication',
    ],
    # Enable DRF pagination
    # 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'DEFAULT_PAGINATION_CLASS': 'drf_defaults.DefaultResultsSetPagination',
    'PAGE_SIZE': 50,
    'DEFAULT_RENDERER_CLASSES': (
    # 'rest_framework.renderers.JSONRenderer', # Swapping out the original renderer
    'lib.drf_renderer.UJSONRenderer',
    'rest_framework.renderers.BrowsableAPIRenderer',
    ),
    'DEFAULT_PARSER_CLASSES': (
    # 'rest_framework.parsers.JSONParser', # Swapping out the original parser
    'lib.drf_parser.UJSONParser',
    'rest_framework.parsers.FormParser',
    'rest_framework.parsers.MultiPartParser'
    ),
    'DEFAULT_FILTER_BACKENDS': (
    'django_filters.rest_framework.DjangoFilterBackend',
    )
    }


    Your settings will of course vary! Cheers!






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35432985%2fdjango-rest-framework-override-page-size-in-viewset%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      13














      I fixed this by creating custom pagination class. and setting desired pagesize in class. I have used this class as pagination_class in my viewset.



      from rest_framework import pagination

      class ExamplePagination(pagination.PageNumberPagination):
      page_size = 2

      class HobbyCategoryViewSet(viewsets.ModelViewSet):
      serializer_class = HobbyCategorySerializer
      queryset = UserHobbyCategory.objects.all()
      pagination_class=ExamplePagination


      I am not sure if there is any easier way for this. this one worked for me. But I think its not good to create new class just to change page_size.



      Edit - simple solution is set it like



      pagination.PageNumberPagination.page_size = 100 


      in ViewSet.



      class HobbyCategoryViewSet(viewsets.ModelViewSet):    
      serializer_class = HobbyCategorySerializer
      queryset = UserHobbyCategory.objects.all()
      pagination.PageNumberPagination.page_size = 100





      share|improve this answer


























      • changing pagination.PageNumberPagination.page_size in different viewsets does not result in different sizes. Only one page_size is set

        – object recognition
        Aug 4 '16 at 7:27
















      13














      I fixed this by creating custom pagination class. and setting desired pagesize in class. I have used this class as pagination_class in my viewset.



      from rest_framework import pagination

      class ExamplePagination(pagination.PageNumberPagination):
      page_size = 2

      class HobbyCategoryViewSet(viewsets.ModelViewSet):
      serializer_class = HobbyCategorySerializer
      queryset = UserHobbyCategory.objects.all()
      pagination_class=ExamplePagination


      I am not sure if there is any easier way for this. this one worked for me. But I think its not good to create new class just to change page_size.



      Edit - simple solution is set it like



      pagination.PageNumberPagination.page_size = 100 


      in ViewSet.



      class HobbyCategoryViewSet(viewsets.ModelViewSet):    
      serializer_class = HobbyCategorySerializer
      queryset = UserHobbyCategory.objects.all()
      pagination.PageNumberPagination.page_size = 100





      share|improve this answer


























      • changing pagination.PageNumberPagination.page_size in different viewsets does not result in different sizes. Only one page_size is set

        – object recognition
        Aug 4 '16 at 7:27














      13












      13








      13







      I fixed this by creating custom pagination class. and setting desired pagesize in class. I have used this class as pagination_class in my viewset.



      from rest_framework import pagination

      class ExamplePagination(pagination.PageNumberPagination):
      page_size = 2

      class HobbyCategoryViewSet(viewsets.ModelViewSet):
      serializer_class = HobbyCategorySerializer
      queryset = UserHobbyCategory.objects.all()
      pagination_class=ExamplePagination


      I am not sure if there is any easier way for this. this one worked for me. But I think its not good to create new class just to change page_size.



      Edit - simple solution is set it like



      pagination.PageNumberPagination.page_size = 100 


      in ViewSet.



      class HobbyCategoryViewSet(viewsets.ModelViewSet):    
      serializer_class = HobbyCategorySerializer
      queryset = UserHobbyCategory.objects.all()
      pagination.PageNumberPagination.page_size = 100





      share|improve this answer















      I fixed this by creating custom pagination class. and setting desired pagesize in class. I have used this class as pagination_class in my viewset.



      from rest_framework import pagination

      class ExamplePagination(pagination.PageNumberPagination):
      page_size = 2

      class HobbyCategoryViewSet(viewsets.ModelViewSet):
      serializer_class = HobbyCategorySerializer
      queryset = UserHobbyCategory.objects.all()
      pagination_class=ExamplePagination


      I am not sure if there is any easier way for this. this one worked for me. But I think its not good to create new class just to change page_size.



      Edit - simple solution is set it like



      pagination.PageNumberPagination.page_size = 100 


      in ViewSet.



      class HobbyCategoryViewSet(viewsets.ModelViewSet):    
      serializer_class = HobbyCategorySerializer
      queryset = UserHobbyCategory.objects.all()
      pagination.PageNumberPagination.page_size = 100






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Aug 15 '18 at 18:44









      radtek

      16.5k69077




      16.5k69077










      answered Feb 16 '16 at 13:04









      MangeshMangesh

      255213




      255213













      • changing pagination.PageNumberPagination.page_size in different viewsets does not result in different sizes. Only one page_size is set

        – object recognition
        Aug 4 '16 at 7:27



















      • changing pagination.PageNumberPagination.page_size in different viewsets does not result in different sizes. Only one page_size is set

        – object recognition
        Aug 4 '16 at 7:27

















      changing pagination.PageNumberPagination.page_size in different viewsets does not result in different sizes. Only one page_size is set

      – object recognition
      Aug 4 '16 at 7:27





      changing pagination.PageNumberPagination.page_size in different viewsets does not result in different sizes. Only one page_size is set

      – object recognition
      Aug 4 '16 at 7:27













      9














      Use page size query params to provide page size dynamically..



      from rest_framework.pagination import PageNumberPagination

      class StandardResultsSetPagination(PageNumberPagination):
      page_size_query_param = 'limit'


      Set Default pagination class in settings



      REST_FRAMEWORK = {'DEFAULT_PAGINATION_CLASS': 'StandardResultsSetPagination',}


      Now in your URL provide limit as a GET parameter..




      http://example.com/list/?limit=100 or 25







      share|improve this answer


























      • Where do you put the 'StandardResultsSetPagination' class so that 'DEFAULT_PAGINATION_CLASS' can see it ?

        – Curtwagner1984
        Aug 1 '16 at 14:29











      • Edited answer...u need to add in settings

        – Naresh
        Aug 2 '16 at 13:52











      • Yes I know, I meant where do you put the class itself so that the setting DEFAULT_PAGINATION_CLASS in settings.py will see it?

        – Curtwagner1984
        Aug 2 '16 at 14:08











      • @Curtwagner1984 you can put anywhere in your project and refer class with package and module name....i would suggest you create some folder for such generic classes..

        – Naresh
        Aug 3 '16 at 9:45
















      9














      Use page size query params to provide page size dynamically..



      from rest_framework.pagination import PageNumberPagination

      class StandardResultsSetPagination(PageNumberPagination):
      page_size_query_param = 'limit'


      Set Default pagination class in settings



      REST_FRAMEWORK = {'DEFAULT_PAGINATION_CLASS': 'StandardResultsSetPagination',}


      Now in your URL provide limit as a GET parameter..




      http://example.com/list/?limit=100 or 25







      share|improve this answer


























      • Where do you put the 'StandardResultsSetPagination' class so that 'DEFAULT_PAGINATION_CLASS' can see it ?

        – Curtwagner1984
        Aug 1 '16 at 14:29











      • Edited answer...u need to add in settings

        – Naresh
        Aug 2 '16 at 13:52











      • Yes I know, I meant where do you put the class itself so that the setting DEFAULT_PAGINATION_CLASS in settings.py will see it?

        – Curtwagner1984
        Aug 2 '16 at 14:08











      • @Curtwagner1984 you can put anywhere in your project and refer class with package and module name....i would suggest you create some folder for such generic classes..

        – Naresh
        Aug 3 '16 at 9:45














      9












      9








      9







      Use page size query params to provide page size dynamically..



      from rest_framework.pagination import PageNumberPagination

      class StandardResultsSetPagination(PageNumberPagination):
      page_size_query_param = 'limit'


      Set Default pagination class in settings



      REST_FRAMEWORK = {'DEFAULT_PAGINATION_CLASS': 'StandardResultsSetPagination',}


      Now in your URL provide limit as a GET parameter..




      http://example.com/list/?limit=100 or 25







      share|improve this answer















      Use page size query params to provide page size dynamically..



      from rest_framework.pagination import PageNumberPagination

      class StandardResultsSetPagination(PageNumberPagination):
      page_size_query_param = 'limit'


      Set Default pagination class in settings



      REST_FRAMEWORK = {'DEFAULT_PAGINATION_CLASS': 'StandardResultsSetPagination',}


      Now in your URL provide limit as a GET parameter..




      http://example.com/list/?limit=100 or 25








      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Aug 2 '16 at 13:52

























      answered Feb 16 '16 at 16:01









      NareshNaresh

      5611421




      5611421













      • Where do you put the 'StandardResultsSetPagination' class so that 'DEFAULT_PAGINATION_CLASS' can see it ?

        – Curtwagner1984
        Aug 1 '16 at 14:29











      • Edited answer...u need to add in settings

        – Naresh
        Aug 2 '16 at 13:52











      • Yes I know, I meant where do you put the class itself so that the setting DEFAULT_PAGINATION_CLASS in settings.py will see it?

        – Curtwagner1984
        Aug 2 '16 at 14:08











      • @Curtwagner1984 you can put anywhere in your project and refer class with package and module name....i would suggest you create some folder for such generic classes..

        – Naresh
        Aug 3 '16 at 9:45



















      • Where do you put the 'StandardResultsSetPagination' class so that 'DEFAULT_PAGINATION_CLASS' can see it ?

        – Curtwagner1984
        Aug 1 '16 at 14:29











      • Edited answer...u need to add in settings

        – Naresh
        Aug 2 '16 at 13:52











      • Yes I know, I meant where do you put the class itself so that the setting DEFAULT_PAGINATION_CLASS in settings.py will see it?

        – Curtwagner1984
        Aug 2 '16 at 14:08











      • @Curtwagner1984 you can put anywhere in your project and refer class with package and module name....i would suggest you create some folder for such generic classes..

        – Naresh
        Aug 3 '16 at 9:45

















      Where do you put the 'StandardResultsSetPagination' class so that 'DEFAULT_PAGINATION_CLASS' can see it ?

      – Curtwagner1984
      Aug 1 '16 at 14:29





      Where do you put the 'StandardResultsSetPagination' class so that 'DEFAULT_PAGINATION_CLASS' can see it ?

      – Curtwagner1984
      Aug 1 '16 at 14:29













      Edited answer...u need to add in settings

      – Naresh
      Aug 2 '16 at 13:52





      Edited answer...u need to add in settings

      – Naresh
      Aug 2 '16 at 13:52













      Yes I know, I meant where do you put the class itself so that the setting DEFAULT_PAGINATION_CLASS in settings.py will see it?

      – Curtwagner1984
      Aug 2 '16 at 14:08





      Yes I know, I meant where do you put the class itself so that the setting DEFAULT_PAGINATION_CLASS in settings.py will see it?

      – Curtwagner1984
      Aug 2 '16 at 14:08













      @Curtwagner1984 you can put anywhere in your project and refer class with package and module name....i would suggest you create some folder for such generic classes..

      – Naresh
      Aug 3 '16 at 9:45





      @Curtwagner1984 you can put anywhere in your project and refer class with package and module name....i would suggest you create some folder for such generic classes..

      – Naresh
      Aug 3 '16 at 9:45











      1














      In case you are trying to change the "page size" of the LimitOffsetPagination class, you have to override the default_limit variable instead of page_size:



      from rest_framework import paginationclass 


      CustomLimitOffsetPagination(pagination.LimitOffsetPagination):
      default_limit = 5





      share|improve this answer




























        1














        In case you are trying to change the "page size" of the LimitOffsetPagination class, you have to override the default_limit variable instead of page_size:



        from rest_framework import paginationclass 


        CustomLimitOffsetPagination(pagination.LimitOffsetPagination):
        default_limit = 5





        share|improve this answer


























          1












          1








          1







          In case you are trying to change the "page size" of the LimitOffsetPagination class, you have to override the default_limit variable instead of page_size:



          from rest_framework import paginationclass 


          CustomLimitOffsetPagination(pagination.LimitOffsetPagination):
          default_limit = 5





          share|improve this answer













          In case you are trying to change the "page size" of the LimitOffsetPagination class, you have to override the default_limit variable instead of page_size:



          from rest_framework import paginationclass 


          CustomLimitOffsetPagination(pagination.LimitOffsetPagination):
          default_limit = 5






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 18 '18 at 16:04









          oneschillingoneschilling

          1188




          1188























              1














              In the case that you want to set a default pagination value, including a max and a param, here is what you do.



              1) Create a drf_defaults.py (or any name you choose). I placed it in same dir as settings.py:



              """
              Django rest framework default pagination
              """
              from rest_framework.pagination import PageNumberPagination


              class DefaultResultsSetPagination(PageNumberPagination):
              page_size = 50
              page_size_query_param = 'page_size'
              max_page_size = 100000


              2) In your settings.py, update REST_FRAMEWORK dict, adding the following:



              'DEFAULT_PAGINATION_CLASS': 'drf_defaults.DefaultResultsSetPagination',


              In the end my REST_FRAMEWORK settings dict looks like:



              # http://www.django-rest-framework.org/api-guide/settings/
              REST_FRAMEWORK = {
              # Use Django's standard `django.contrib.auth` permissions,
              # or allow read-only access for unauthenticated users.
              'DEFAULT_PERMISSION_CLASSES': [
              # 'rest_framework.permissions.AllowAny', # Use to disable api auth
              # 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
              'permissions.IsAuthenticatedWriteOrReadOnly',
              ],
              'DEFAULT_AUTHENTICATION_CLASSES': [
              # 'oauth2_provider.contrib.rest_framework.OAuth2Authentication', # Own oauth server
              'client_authentication.ApiTokenAuthentication',
              'rest_framework.authentication.SessionAuthentication',
              'rest_framework.authentication.BasicAuthentication',
              ],
              # Enable DRF pagination
              # 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
              'DEFAULT_PAGINATION_CLASS': 'drf_defaults.DefaultResultsSetPagination',
              'PAGE_SIZE': 50,
              'DEFAULT_RENDERER_CLASSES': (
              # 'rest_framework.renderers.JSONRenderer', # Swapping out the original renderer
              'lib.drf_renderer.UJSONRenderer',
              'rest_framework.renderers.BrowsableAPIRenderer',
              ),
              'DEFAULT_PARSER_CLASSES': (
              # 'rest_framework.parsers.JSONParser', # Swapping out the original parser
              'lib.drf_parser.UJSONParser',
              'rest_framework.parsers.FormParser',
              'rest_framework.parsers.MultiPartParser'
              ),
              'DEFAULT_FILTER_BACKENDS': (
              'django_filters.rest_framework.DjangoFilterBackend',
              )
              }


              Your settings will of course vary! Cheers!






              share|improve this answer




























                1














                In the case that you want to set a default pagination value, including a max and a param, here is what you do.



                1) Create a drf_defaults.py (or any name you choose). I placed it in same dir as settings.py:



                """
                Django rest framework default pagination
                """
                from rest_framework.pagination import PageNumberPagination


                class DefaultResultsSetPagination(PageNumberPagination):
                page_size = 50
                page_size_query_param = 'page_size'
                max_page_size = 100000


                2) In your settings.py, update REST_FRAMEWORK dict, adding the following:



                'DEFAULT_PAGINATION_CLASS': 'drf_defaults.DefaultResultsSetPagination',


                In the end my REST_FRAMEWORK settings dict looks like:



                # http://www.django-rest-framework.org/api-guide/settings/
                REST_FRAMEWORK = {
                # Use Django's standard `django.contrib.auth` permissions,
                # or allow read-only access for unauthenticated users.
                'DEFAULT_PERMISSION_CLASSES': [
                # 'rest_framework.permissions.AllowAny', # Use to disable api auth
                # 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
                'permissions.IsAuthenticatedWriteOrReadOnly',
                ],
                'DEFAULT_AUTHENTICATION_CLASSES': [
                # 'oauth2_provider.contrib.rest_framework.OAuth2Authentication', # Own oauth server
                'client_authentication.ApiTokenAuthentication',
                'rest_framework.authentication.SessionAuthentication',
                'rest_framework.authentication.BasicAuthentication',
                ],
                # Enable DRF pagination
                # 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
                'DEFAULT_PAGINATION_CLASS': 'drf_defaults.DefaultResultsSetPagination',
                'PAGE_SIZE': 50,
                'DEFAULT_RENDERER_CLASSES': (
                # 'rest_framework.renderers.JSONRenderer', # Swapping out the original renderer
                'lib.drf_renderer.UJSONRenderer',
                'rest_framework.renderers.BrowsableAPIRenderer',
                ),
                'DEFAULT_PARSER_CLASSES': (
                # 'rest_framework.parsers.JSONParser', # Swapping out the original parser
                'lib.drf_parser.UJSONParser',
                'rest_framework.parsers.FormParser',
                'rest_framework.parsers.MultiPartParser'
                ),
                'DEFAULT_FILTER_BACKENDS': (
                'django_filters.rest_framework.DjangoFilterBackend',
                )
                }


                Your settings will of course vary! Cheers!






                share|improve this answer


























                  1












                  1








                  1







                  In the case that you want to set a default pagination value, including a max and a param, here is what you do.



                  1) Create a drf_defaults.py (or any name you choose). I placed it in same dir as settings.py:



                  """
                  Django rest framework default pagination
                  """
                  from rest_framework.pagination import PageNumberPagination


                  class DefaultResultsSetPagination(PageNumberPagination):
                  page_size = 50
                  page_size_query_param = 'page_size'
                  max_page_size = 100000


                  2) In your settings.py, update REST_FRAMEWORK dict, adding the following:



                  'DEFAULT_PAGINATION_CLASS': 'drf_defaults.DefaultResultsSetPagination',


                  In the end my REST_FRAMEWORK settings dict looks like:



                  # http://www.django-rest-framework.org/api-guide/settings/
                  REST_FRAMEWORK = {
                  # Use Django's standard `django.contrib.auth` permissions,
                  # or allow read-only access for unauthenticated users.
                  'DEFAULT_PERMISSION_CLASSES': [
                  # 'rest_framework.permissions.AllowAny', # Use to disable api auth
                  # 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
                  'permissions.IsAuthenticatedWriteOrReadOnly',
                  ],
                  'DEFAULT_AUTHENTICATION_CLASSES': [
                  # 'oauth2_provider.contrib.rest_framework.OAuth2Authentication', # Own oauth server
                  'client_authentication.ApiTokenAuthentication',
                  'rest_framework.authentication.SessionAuthentication',
                  'rest_framework.authentication.BasicAuthentication',
                  ],
                  # Enable DRF pagination
                  # 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
                  'DEFAULT_PAGINATION_CLASS': 'drf_defaults.DefaultResultsSetPagination',
                  'PAGE_SIZE': 50,
                  'DEFAULT_RENDERER_CLASSES': (
                  # 'rest_framework.renderers.JSONRenderer', # Swapping out the original renderer
                  'lib.drf_renderer.UJSONRenderer',
                  'rest_framework.renderers.BrowsableAPIRenderer',
                  ),
                  'DEFAULT_PARSER_CLASSES': (
                  # 'rest_framework.parsers.JSONParser', # Swapping out the original parser
                  'lib.drf_parser.UJSONParser',
                  'rest_framework.parsers.FormParser',
                  'rest_framework.parsers.MultiPartParser'
                  ),
                  'DEFAULT_FILTER_BACKENDS': (
                  'django_filters.rest_framework.DjangoFilterBackend',
                  )
                  }


                  Your settings will of course vary! Cheers!






                  share|improve this answer













                  In the case that you want to set a default pagination value, including a max and a param, here is what you do.



                  1) Create a drf_defaults.py (or any name you choose). I placed it in same dir as settings.py:



                  """
                  Django rest framework default pagination
                  """
                  from rest_framework.pagination import PageNumberPagination


                  class DefaultResultsSetPagination(PageNumberPagination):
                  page_size = 50
                  page_size_query_param = 'page_size'
                  max_page_size = 100000


                  2) In your settings.py, update REST_FRAMEWORK dict, adding the following:



                  'DEFAULT_PAGINATION_CLASS': 'drf_defaults.DefaultResultsSetPagination',


                  In the end my REST_FRAMEWORK settings dict looks like:



                  # http://www.django-rest-framework.org/api-guide/settings/
                  REST_FRAMEWORK = {
                  # Use Django's standard `django.contrib.auth` permissions,
                  # or allow read-only access for unauthenticated users.
                  'DEFAULT_PERMISSION_CLASSES': [
                  # 'rest_framework.permissions.AllowAny', # Use to disable api auth
                  # 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
                  'permissions.IsAuthenticatedWriteOrReadOnly',
                  ],
                  'DEFAULT_AUTHENTICATION_CLASSES': [
                  # 'oauth2_provider.contrib.rest_framework.OAuth2Authentication', # Own oauth server
                  'client_authentication.ApiTokenAuthentication',
                  'rest_framework.authentication.SessionAuthentication',
                  'rest_framework.authentication.BasicAuthentication',
                  ],
                  # Enable DRF pagination
                  # 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
                  'DEFAULT_PAGINATION_CLASS': 'drf_defaults.DefaultResultsSetPagination',
                  'PAGE_SIZE': 50,
                  'DEFAULT_RENDERER_CLASSES': (
                  # 'rest_framework.renderers.JSONRenderer', # Swapping out the original renderer
                  'lib.drf_renderer.UJSONRenderer',
                  'rest_framework.renderers.BrowsableAPIRenderer',
                  ),
                  'DEFAULT_PARSER_CLASSES': (
                  # 'rest_framework.parsers.JSONParser', # Swapping out the original parser
                  'lib.drf_parser.UJSONParser',
                  'rest_framework.parsers.FormParser',
                  'rest_framework.parsers.MultiPartParser'
                  ),
                  'DEFAULT_FILTER_BACKENDS': (
                  'django_filters.rest_framework.DjangoFilterBackend',
                  )
                  }


                  Your settings will of course vary! Cheers!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 18:32









                  radtekradtek

                  16.5k69077




                  16.5k69077






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35432985%2fdjango-rest-framework-override-page-size-in-viewset%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python