[英]Django rest framework pagination with custom API view
I am trying to add pagination into my project, couldn't find any clear documentation or tutorial. 我试图在我的项目中添加分页,找不到任何明确的文档或教程。
I have a list of offices 我有一份办公室名单
models Office.py 模型Office.py
class Office(Model):
name = CharField(_("name"), default=None, max_length=255, null=True)
email = EmailField(_("email"), default=None, max_length=255, null=True)
description = TextField(_("description"), default=None, null=True)
Serializer 串行
class OfficeSerializer(ModelSerializer):
id = IntegerField(read_only=True)
name = CharField(read_only=True)
email = URLField(read_only=True)
description = CharField(read_only=True)
class Meta:
model = Office
fields = ("id", "name", "email", "description")
views.py views.py
@api_view(["GET"])
@permission_classes((AllowAny,))
def offices(request):
instance = Office.objects.filter()[:10]
serializer = OfficeSerializer(instance, many=True)
return Response(serializer.data)
Any help with returning Office list with pagination ? 使用分页返回Office列表的任何帮助?
http://www.django-rest-framework.org/api-guide/pagination/ http://www.django-rest-framework.org/api-guide/pagination/
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. 如果您使用常规APIView,则需要自己调用分页API以确保返回分页响应。 See the source code for the mixins.ListModelMixin and generics.GenericAPIView classes for an example. 有关示例,请参阅mixins.ListModelMixin和generics.GenericAPIView类的源代码。
https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/mixins.py#L35 https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/generics.py#L166 https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/mixins.py#L35 https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/generics的.py#L166
so I would suggest something like: 所以我会建议像:
@api_view(["GET"])
@permission_classes((AllowAny,))
def offices(request):
pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
paginator = pagination_class()
queryset = Office.objects.all()
page = paginator.paginate_queryset(queryset, request)
serializer = OfficeSerializer(page, many=True)
return paginator.get_paginated_response(serializer.data)
http://www.django-rest-framework.org/api-guide/pagination/ http://www.django-rest-framework.org/api-guide/pagination/
GET https://api.example.org/accounts/?limit=100&offset=400
Response: 响应:
HTTP 200 OK
{
"count": 1023
"next": "https://api.example.org/accounts/?limit=100&offset=500",
"previous": "https://api.example.org/accounts/?limit=100&offset=300",
"results": [
…
]
}
Example of settings.py
settings.py
示例
REST_FRAMEWORK = {
'PAGE_SIZE': 10,
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
'DEFAULT_PAGINATION_CLASS':
'rest_framework_json_api.pagination.PageNumberPagination',
'DEFAULT_PARSER_CLASSES': (
'rest_framework_json_api.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework_json_api.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.