简体   繁体   English

Django Rest Framework,具有自定义分页的Rest API

[英]Django Rest Framework, Rest API with custom pagination

I am using Django Rest Framework. 我正在使用Django Rest Framework。
I have the following situation: 我有以下情况:

I have a table with some elements (for example 10k elements). 我有一个包含一些元素的表(例如10k元素)。
I also have an unique index in this model: 我在这个模型中也有一个独特的索引:

....
Element i-1
Element i
Element i+1
Element i+2
....

I need to have a REST API with the following response: 我需要一个带有以下响应的REST API:

GET Elements by center Index: i 按中心指数获取元素: i

and I need to return a set of result that contains N elements that are before and N elements that are after the element i . 我需要返回一组结果的包含有N个单元之前和N是所述元素i之后的元素。

Using the pagination is possible to navigate in this set of results: Using PREV I can request the previous page and using NEXT I can request the next page... Example: 使用分页可以在这组结果中导航:使用PREV我可以请求上一页并使用NEXT我可以请求下一页...示例:

Get elements by center index = i 按中心索引= i获取元素

PREV:....

Result:
[
Element k
Element k+1
......
Element i
......
Element w
Element w+1
]
NEXT:....

In DRF documentation on pagination is stated that it is possible to write a custom pagination method: DRF文档中,分页表明可以编写自定义分页方法:

To create a custom pagination serializer class you should subclass pagination.BasePagination and override the paginate_queryset(self, queryset, request, view=None) and get_paginated_response(self, data) methods: 要创建自定义分页序列化程序类,您应该get_paginated_response(self, data) pagination.BasePagination并覆盖paginate_queryset(self, queryset, request, view=None)get_paginated_response(self, data)方法:

  • The paginate_queryset method is passed the initial queryset and should return an iterable object that contains only the data in the >requested page. paginate_queryset方法传递给初始查询集,并且应该返回一个只包含>请求页面中的数据的可迭代对象。
  • The get_paginated_response method is passed the serialized page data and should return a Response instance get_paginated_response方法传递序列化页面数据,并应返回Response实例

So we are going to do just that: 所以我们将这样做:

BASIC EXAMPLE 基本范例

from rest_framework.pagination import BasePagination


class CenterPointPagination(BasePagination):
    default_center = 0
    default_radius = 5
    center_query_param = 'center'
    radius_query_param = 'radius'        

    def paginate_queryset(self, queryset, request, view=None):
        center = request.get(center_query_param, default_center)
        radius = request.get(radius_query_param, default_radius)

        start = center - radius if center - radius > 0 else 0
        finish = center + radius
        return list(queryset[start:finish])

    def get_paginated_resposne(self, data):
        return Response(OrderedDict([
            'center': center,
            'radius': radius,
            'results': data
        ]))

That pretty much is what you need as a base. 这几乎是你作为基础所需要的。
You will have to customize it further to suite your needs. 您必须进一步定制它以满足您的需求。
To get some ideas on how to do that, have a look at the source code of 要了解如何做到这一点,请查看源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM