2

I have try to paginate my data.. but this is not work , I'm still getting all data from DataBase this is views.py :

class User_apiView(APIView):
    pagination_class=PageNumberPagination
    def get(self, request):
        user = User.objects.all() 
        # pagination_class=PageNumberPagination
        serializer = TripSerializer(user, many = True)
        return Response(serializer.data)

this is settings.py :

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 2,
}

this the data I recieve in this url http://127.0.0.1:8000/api/users/?PAGE=4&PAGE_SIZE=1



HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "id": 115,
        "is_normaluser": null,
        "is_agency": null,
        "last_login": "2022-02-11T20:28:13.506519Z",
        "is_superuser": true,
        "first_name": "qaz",
    },
    {
        "id": 115,
        "is_normaluser": null,
        "is_agency": null,
        "last_login": "2022-02-11T20:28:13.506519Z",
        "is_superuser": true,
        "first_name": "qaz",
    },
    {
        "id": 115,
        "is_normaluser": null,
        "is_agency": null,
        "last_login": "2022-02-11T20:28:13.506519Z",
        "is_superuser": true,
        "first_name": "qaz",
    },
   
]
2
  • Because you override the get method, you omit the pagination part... Commented Apr 2, 2022 at 17:23
  • 1
    From the pagination docs - Pagination is only performed automatically if you're using the generic views or viewsets. If you're using a regular APIView, you'll need to call into the pagination API yourself to ensure you return a paginated response. See the source code for the mixins.ListModelMixin and generics.GenericAPIView classes for an example Commented Apr 2, 2022 at 17:23

1 Answer 1

1

Pagination, etc. only works for APIViews that have implemented the logic for pagination, for example a ListAPIView. You this can work with:

from rest_framework.generics import ListAPIView

class User_apiView(ListAPIView):
    pagination_class = PageNumberPagination
    queryset = User.objects.all()
    serializer_class = TripSerializer

This will implement the .get(…) method that will fetch the paginated queryset, and use the serializer to serialize the data and put it into a response.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.