user12551055
user12551055

Reputation:

Django Rest Framework pagination not working

These are my version

Django==3.0.2
djangorestframework==3.11.0

and this is my setting

REST_FRAMEWORK = {
        'DEFAULT_PERMISSION_CLASSES': [
            'rest_framework.permissions.IsAuthenticated',
        ],
        'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
        'PAGE_SIZE': 10
    }

and this is my views:

class CostList(ListCreateAPIView):
    serializer_class = CostSerializers

    def get_queryset(self):
        cost = Cost.objects.filter(
            id='filtered with one of my id'
        )
        return cost

this is my serializer:

class CostSerializers(ModelSerializer):
    class Meta:
        model = Cost
        fields = '__all__'

Everything is working fine but the only issue is pagination. I have 100+ entry in cost model and I see it is rendering all the entry together, not paginating item following my settings

Upvotes: 1

Views: 1475

Answers (1)

sandeshdaundkar
sandeshdaundkar

Reputation: 903

class CostList(ListCreateAPIView):
    serializer_class = CostSerializers

    def get_queryset(self):
        cost = Cost.objects.filter(id='filtered with one of my id')
        return cost

    def get(self, request, *args, **kwargs):
        qs = self.get_queryset()
        page = self.paginate_queryset(qs)
        return self.get_paginated_response(page)

Try this.

Upvotes: 2

Related Questions