Python rest_framework.pagination.PageNumberPagination() Examples

The following are 6 code examples for showing how to use rest_framework.pagination.PageNumberPagination(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module rest_framework.pagination , or try the search function .

Example 1
Project: py2swagger   Author: Arello-Mobile   File: pagination.py    License: MIT License 6 votes vote down vote up
def get_pagination_introspector(view, si=None):
    """
    Create pagination introspector based on view
    :param view: DjangoRestFramework view
    :param si: SerializerIntrospector
    :return: PaginationIntrospector
    :rtype: BasePaginationIntrospector
    """
    if getattr(view, 'pagination_class', None):
        # DjangoRestFramework 3.0 pagination style with pagination class
        pagination_class = view.pagination_class
        from rest_framework import pagination
        if pagination_class == pagination.PageNumberPagination:
            return PageNumberPaginationIntrospector(view, pagination_class, si=si)
        elif pagination_class == pagination.LimitOffsetPagination:
            return LimitOffsetPaginationIntrospector(view, pagination_class, si=si)
        elif pagination_class == pagination.CursorPagination:
            return CursorPaginationIntrospector(view, pagination_class, si=si)
        else:
            return BasePaginationIntrospector(view, pagination_class, si=si)
    else:
        # Unrecognized view type
        return BasePaginationIntrospector(si=si) 
Example 2
Project: onehome-server   Author: michwh   File: views.py    License: MIT License 6 votes vote down vote up
def get(self, request):
        if request.user.is_authenticated:
            # 获取所有数据
            roles = Product.objects.all()
            # 创建分页对象
            pg = PageNumberPagination()
            # pg = MyLimitOffsetPagination()
            # 获取分页的数据
            page_roles = pg.paginate_queryset(queryset=roles, request=request, view=self)
            # 对数据进行序列化
            ser = ProductSerializer(instance=page_roles, many=True)
            # print('ser.data的类型:' + str(ser.data))
            msg = sort_out_list(request, ser.data)
            return Response(msg, status=HTTP_200_OK)
        else:
            msg = {
                'stateCode': 201,
                'msg': '没有访问权限'
            }
            return Response(msg, 201)


# 获取搜索列表,暂时不分页 
Example 3
Project: drf_openapi   Author: limdauto   File: entities.py    License: MIT License 6 votes vote down vote up
def get_paginator_serializer(self, view, child_serializer_class):
        class BaseFakeListSerializer(serializers.Serializer):
            results = child_serializer_class(many=True)

        class FakePrevNextListSerializer(BaseFakeListSerializer):
            next = URLField()
            previous = URLField()

        # Validate if the view has a pagination_class
        if not (hasattr(view, 'pagination_class')) or view.pagination_class is None:
            return BaseFakeListSerializer

        pager = view.pagination_class
        if hasattr(pager, 'default_pager'):
            # Must be a ProxyPagination
            pager = pager.default_pager

        if issubclass(pager, (PageNumberPagination, LimitOffsetPagination)):
            class FakeListSerializer(FakePrevNextListSerializer):
                count = IntegerField()
            return FakeListSerializer
        elif issubclass(pager, CursorPagination):
            return FakePrevNextListSerializer

        return BaseFakeListSerializer 
Example 4
Project: drf-schema-adapter   Author: drf-forms   File: test_unit.py    License: MIT License 6 votes vote down vote up
def test_viewset_factory(self):
        viewset = self.endpoint.get_viewset()
        self.assertEqual(viewset.serializer_class, self.endpoint.get_serializer())

        for backend in (DjangoFilterBackend, filters.SearchFilter):
            self.assertNotIn(backend, viewset.filter_backends)

        self.assertEqual(viewset.__name__, 'ProductViewSet')

        viewset = self.alternate_endpoint.get_viewset()

        for attr in ('permission_classes', 'filter_fields', 'search_fields', 'ordering_fields'):
            self.assertEqual(list(getattr(viewset, attr)), list(getattr(self, attr)))

        for backend in ('DjangoFilterBackend', 'SearchFilter', 'OrderingFilter'):
            self.assertIn(backend, [be.__name__ for be in viewset.filter_backends])

        self.assertEqual(viewset.pagination_class.__name__, 'ProductPagination')
        self.assertTrue(issubclass(
            viewset.pagination_class,
            pagination.PageNumberPagination
        )) 
Example 5
Project: drf-haystack   Author: rhblind   File: test_viewsets.py    License: MIT License 6 votes vote down vote up
def setUp(self):

        MockPersonIndex().reindex()

        class Serializer1(HaystackSerializer):

            class Meta:
                fields = ["firstname", "lastname"]
                index_classes = [MockPersonIndex]

        class NumberPagination(PageNumberPagination):

            page_size = 5

        class ViewSet1(HaystackViewSet):

            index_models = [MockPerson]
            serializer_class = Serializer1
            pagination_class = NumberPagination

        self.view1 = ViewSet1 
Example 6
Project: drf-schema-adapter   Author: drf-forms   File: factories.py    License: MIT License 5 votes vote down vote up
def pagination_factory(endpoint):
    pg_cls_name = '{}Pagination'.format(endpoint.model.__name__)

    page_size = getattr(endpoint, 'page_size', None)
    pg_cls_attrs = {
        'page_size': page_size if page_size is not None else settings.REST_FRAMEWORK.get('PAGE_SIZE', 50),
    }

    if hasattr(endpoint, 'pagination_template'):
        pg_cls_attrs['template'] = endpoint.pagination_template

    BasePagination = getattr(endpoint, 'base_pagination_class', pagination.PageNumberPagination)
    if issubclass(BasePagination, pagination.PageNumberPagination):
        pg_cls_attrs['page_size_query_param'] = getattr(endpoint, 'page_size_query_param', 'page_size')
        for param in ('django_paginator_class', 'page_query_param', 'max_page_size', 'last_page_string',
                      'page_size'):
            if getattr(endpoint, param, None) is not None:
                pg_cls_attrs[param] = getattr(endpoint, param)
    elif issubclass(BasePagination, pagination.LimitOffsetPagination):
        pg_cls_attrs.pop('page_size')
        for param in ('default_limit', 'limit_query_param', 'offset_query_param', 'max_limit'):
            if getattr(endpoint, param, None) is not None:
                pg_cls_attrs[param] = getattr(endpoint, param)
    elif issubclass(BasePagination, pagination.CursorPagination):
        for param in ('page_size', 'cursor_query_param', 'ordering'):
            if getattr(endpoint, param, None) is not None:
                pg_cls_attrs[param] = getattr(endpoint, param)
    else:
        raise ImproperlyConfigured('base_pagination_class needs to be a subclass of one of the following:'
                                   'PageNumberPagination, LimitOffsetPagination, CursorPagination')

    return type(pg_cls_name, (BasePagination, ), pg_cls_attrs)