Define page_size page breaks for each view in Django Rest Framework

Since version 3.3could not determine page_size in the view, as it moved to the paginator class. related deviations

Our API has different page_sizes defined for different views, and it feels ambiguous by adding new paginator subclasses to overwrite the page_size attribute. I cannot create an instance of the paginator class in the view definition and use the method __init__as it created here . I could overwrite this and make it a method that returns an instance created using the correct parameters, but since its name is not get_pagination_class, it might not be a good idea.

My question is, what would be the cleanest way to dynamically create a paginator class with an appropriate set of attributes page_size?

I saw this question, and I would like to avoid both of its solutions.

+4
source share
2 answers

You can achieve something relatively clean:


  • Assuming you set the pagination to class using pagination_class.
  • Assuming you keep the attribute page_sizein your class.

  • In settings.pyadd global pagination setting:

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

    You can handle with this all kinds that have the same page size.

  • For any other species / species:

    class ACustomViewSet(mixins.ListModelMixin, 
                         mixins.DestroyModelMixin, 
                         viewsets.GenericViewSet):
    
        pagination_class = PageNumberPagination
        page_size = N
        pagination_class.page_size = self.page_size
    

    :

    def list(self, request, *args, **kwargs):
        self.pagination_class.page_size = self.page_size 
        ...
    
+4

. .

from rest_framework import pagination

class CustomPageNumberPagination(pagination.PageNumberPagination):
    """Custom page number pagination."""

    page_size = 30
    max_page_size = 10000
    page_size_query_param = 'page_size'

    def get_page_size(self, request):
        """Get page size."""
        # On certain pages, force custom/max page size.
        try:
            view = request.parser_context['view']
            if view.action in [
                'custom_page_size_view_1',
                'custom_page_size_view_2',
                # ...
                'custom_page_size_view_n',
            ]:
                return self.max_page_size
        except:
            pass

        return super(CustomPageNumberPagination, self).get_page_size(request)

from rest_framework.viewsets import ModelViewSet
from rest_framework.decorators import list_route
from .pagination import CustomPageNumberPagination

class MyView(ModelViewSet):

    pagination_class = CustomPageNumberPagination

    @list_route()
    def custom_page_size_view_1(self, request):
        """Custom page size view 1"""

    @list_route()
    def custom_page_size_view_2(self, request):
        """Custom page size view 2"""

    @list_route()
    def custom_page_size_view_3(self, request):
        """Custom page size view 3"""
+1

Source: https://habr.com/ru/post/1649032/


All Articles