使用邮递员在Django中进行分页

时间:2018-08-06 11:12:13

标签: django django-rest-framework postman backend

我正在使用Django 2.1版,并且正在使用Postman测试我的项目。 这是我的views.py文件:-

from rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIView
from rest_framework.permissions import AllowAny


# Add + List
# Retrieve Update Destroy
# List: Pagination

class ShowAddaView(ListCreateAPIView):

    from .serializers import AddAddaSerializer

    from .models import Adda

    permission_classes = (AllowAny, )
    serializer_class = AddAddaSerializer
    queryset = Adda.objects.all()


class RetrieveAddaView(RetrieveUpdateDestroyAPIView):
    from .serializers import AddAddaSerializer

    from .models import Adda

    permission_classes = (AllowAny,)
    serializer_class = AddAddaSerializer
    queryset = Adda.objects.all()

我在邮递员中应用GET和POST方法。我的序列化器文件如下所示:

class AddAddaSerializer(serializers.ModelSerializer):
    class Meta:
        from .models import Adda
        model = Adda
        fields = '__all__'


class UpdateAddaSerializer(serializers.ModelSerializer):
    mobile = serializers.CharField(required=False)

    class Meta:
        from .models import Adda
        model = Adda
        fields = '__all__'

现在,我需要使用Postman中的GET方法进行分页访问我的数据。 谁能告诉我实现该目标需要做哪些更改。

1 个答案:

答案 0 :(得分:2)

将此设置添加到您的settings.py文件中,

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10  # Change this value according to your need
}



或您可以通过 pagination_class 作为

在“查看级别”中添加分页类
from rest_framework.pagination import PageNumberPagination


class ShowAddaView(ListCreateAPIView):
    # your code
    pagination_class = PageNumberPagination


class RetrieveAddaView(RetrieveUpdateDestroyAPIView):
    # your code
    pagination_class = PageNumberPagination

有关更多详细信息,请参见Pagination in DRF