Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

LimitOffsetPagination with viewsets.ViewSet in Django Rest Framework

Can we use LimitOffsetPagination with viewsets.ViewSet in Django Rest Framework.

settings.py:

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework.authentication.TokenAuthentication",
    ],
    "DEFAULT_PERMISSION_CLASSES": [
        "rest_framework.permissions.IsAuthenticated",
    ],
    "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
    "PAGE_SIZE": 100,
}

Below is how my list view looks:

class UserViewSet(viewsets.ViewSet):

    pagination_class = LimitOffsetPagination

    def get_permissions(self):
        """
        Instantiates and returns the list of permissions that this view requires.
        """
        permission_classes = [IsAuthenticated]

        if self.action == "list" or self.action == "retrieve":
            permission_classes.append(UserReadPermission)
        elif self.action == "create" or self.action == "update":
            permission_classes.append(UserWritePermission)
        elif self.action == "destroy":
            permission_classes.append(UserDeletePermission)

        return [permission() for permission in permission_classes]

    def list(self, request):

        serializer = UserIndexSerializer(data=request.query_params)

        if serializer.is_valid():
            users = User.objects.all()
            serializer = UserGetSerializer(users, many=True)

            response = {"users": serializer.data, "total": len(serializer.data)}

            return AppResponse.success("User list found.", response)

        return AppResponse.error(serializer.errors, None, http_error_code=400)

    def create(self, request):
        pass

    def retrieve(self, request, pk=None):
        pass

    def update(self, request, pk=None):
        pass

    def partial_update(self, request, pk=None):
        pass

    def destroy(self, request, pk=None):
        pass

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use django-rest default pagination style for entire app using below code in settings.py

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 50 }

If you need pagination per view you can make it as per documentation

class StandardResultsSetPagination(PageNumberPagination):
    page_size = 100
    page_size_query_param = 'page_size'
    max_page_size = 1000

Then Use it in your View

class viewclass(APIView):
queryset = customquery
serializer_class = serializerclass
pagination_class = LargeResultsSetPagination

Using Parameters can also help like GET https://api.example.org/accounts/?limit=100&offset=400


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...