Django Rest框架分页设置 - 内容范围 [英] Django Rest Framework Pagination Settings - Content-Range
问题描述
6.30.15 - 我如何使这个问题更好,更有助于其他人?反馈应该是有帮助的谢谢!
我正在使用DRF作为Dojo / Dgrid Web应用程序的服务器端。 Dojo需要来自服务器的内容范围或范围响应。目前,它不发送任何内容,因此dgrid.grid的分页工作不正常。
I am using DRF as server side to a Dojo/Dgrid web application. Dojo needs a content-range or range response from the server. Currently it doesn't send any and so the pagination for dgrid.grid isn't working properly.
在DRF文档中,它指出响应标头中包含的分页链接,如内容-Range或链接。但是,如何设置DRF API来执行此操作并没有给出明确的过程。我还是比较新的web应用程序开发。任何帮助将不胜感激!
In the DRF documentation it states" Pagination links that are included in response headers, such as Content-Range or Link." But doesn't give a clear process on HOW to set the DRF API to do this. I am still relatively new to web app development. Any help would be appreciated!
推荐答案
1。包括链接
标题作为回应:
1. Including Link
Header in response:
要包含链接
header在你的响应中,你需要创建一个自定义分页序列化器类,这应该是子类
pagination.BasePagination
并覆盖 get_paginated_response(自我,数据)
方法。
示例(取自 docs ):
Example (taken from docs):
假设我们要用包含 Link
头中的下一个和前一个链接的修改格式替换默认分页输出样式,我们覆盖 get_paginated_response( )
。
Suppose we want to replace the default pagination output style with a modified format that includes the next and previous links in a Link
header, we override the get_paginated_response()
.
class LinkHeaderPagination(pagination.PageNumberPagination):
def get_paginated_response(self, data):
next_url = self.get_next_link()
previous_url = self.get_previous_link()
if next_url is not None and previous_url is not None:
link = '<{next_url}; rel="next">, <{previous_url}; rel="prev">'
elif next_url is not None:
link = '<{next_url}; rel="next">'
elif previous_url is not None:
link = '<{previous_url}; rel="prev">'
else:
link = ''
link = link.format(next_url=next_url, previous_url=previous_url)
headers = {'Link': link} if link else {}
return Response(data, headers=headers)
此后,我们需要在我们的设置中包含此分页类,以便DRF使用它,而不是默认的分页类。
After this, we need to include this pagination class in our settings so that it is used by DRF instead of the default pagination classes.
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'my_project.apps.core.pagination.LinkHeaderPagination',
'PAGE_SIZE': 100
}
列表端点的API响应现在将包含一个 Link
头。
API responses for list endpoints will now include a Link
header.
2。包含内容范围
标题作为回应:
2. Including Content-Range
Header in response:
您还可以发送内容范围
标题而不是链接
。只需创建一个标题字典,其中包含 Content-Range
作为键和值,返回多少个项目以及存在多少个项目。
You can also send Content-Range
header instead of Link
. Just create a headers dictionary with Content-Range
as the key and value as how many items are being returned and how many total items exist.
例如:
class ContentRangeHeaderPagination(pagination.PageNumberPagination):
def get_paginated_response(self, data):
total_items = self.page.paginator.count
item_starting_index = self.page.start_index() - 1 # In a page, indexing starts from 1
item_ending_index = self.page.end_index() - 1
content_range = 'items {0}-{1}/{2}'.format(item_starting_index, item_ending_index, total_items)
headers = {'Content-Range': content_range}
return Response(data, headers=headers)
假设这是收到的标题:
Content-Range: items 0-9/50
这告诉我们r请求具有内容范围
标题,值为项目0-9 / 50
。这表示前10个项目从总计 50
中退回。
This tells us that the response has a Content-Range
header with value as items 0-9/50
. This indicates that first 10 items are returned out of total 50
.
您还可以使用 *
而不是总数。的项目,如果计算总额是昂贵的。
You can also use *
instead of total no. of items if calculating total is expensive.
Content-Range: items 0-9/* # Use this if total is expensive to calculate
这篇关于Django Rest框架分页设置 - 内容范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!