Django 如何使用CursorPagination获取当前光标?
默认情况下,CursorPagination为您提供下一个和上一个游标。有没有办法获取当前光标?CUSORPAGIATION类只需从查询字符串中读取值并解码以供内部使用。如果您想知道刚刚提出的请求的当前位置,您也可以这样做。由于paginator实例在使用后被抛出,因此您无法查看它 即使可以,您也会得到“解码”光标值,您必须对其重新编码,以便稍后再次发送Django 如何使用CursorPagination获取当前光标?,django,django-rest-framework,pagination,Django,Django Rest Framework,Pagination,默认情况下,CursorPagination为您提供下一个和上一个游标。有没有办法获取当前光标?CUSORPAGIATION类只需从查询字符串中读取值并解码以供内部使用。如果您想知道刚刚提出的请求的当前位置,您也可以这样做。由于paginator实例在使用后被抛出,因此您无法查看它 即使可以,您也会得到“解码”光标值,您必须对其重新编码,以便稍后再次发送 # from rest_framework/pagination.py@CursorPagination.paginate_queryset
# from rest_framework/pagination.py@CursorPagination.paginate_queryset
encoded = request.query_params.get(self.cursor_query_param)
# read it yourself somewhere in your view/viewset
current_value = request.query_params.get('cursor')
如果您想更改一般行为,只需进行自定义实现并返回附加字段。在视图上(如果是全局设置,则在设置中)将其设置为您的分页类
未经测试示例代码:
class FancyCursorPagination(CusorPagination):
def get_paginated_response(self, data):
return Response(OrderedDict([
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('current', self.get_current_link()),
('results', data)
]))
def get_current_link(self):
"""
Return a link to the current position.
- self.cursor set in the paginate_queryset() method.
To return only the query parameter in this field, use:
- return request.query_params.get(self.cursor_query_param, None)
"""
if self.cursor:
return self.encode_cursor(self.cursor)
else:
# cursor will be None on the first call
return None
def get_paginated_response_schema(self, schema):
new_schema = super().get_paginated_response_schema(schema)
new_schema["properties"]["current"] = {
"type": "string",
"nullable": True
}
return new_schema
class MyApiView(APIView):
pagination_class = FancyCursorPagination
CusorPagination
类只是从querystring中读取值并对其进行解码以供内部使用。如果您想知道刚刚提出的请求的当前位置,您也可以这样做。由于paginator实例在使用后被抛出,因此您无法查看它
即使可以,您也会得到“解码”光标值,您必须对其重新编码,以便稍后再次发送
# from rest_framework/pagination.py@CursorPagination.paginate_queryset
encoded = request.query_params.get(self.cursor_query_param)
# read it yourself somewhere in your view/viewset
current_value = request.query_params.get('cursor')
如果您想更改一般行为,只需进行自定义实现并返回附加字段。在视图上(如果是全局设置,则在设置中)将其设置为您的分页类
未经测试示例代码:
class FancyCursorPagination(CusorPagination):
def get_paginated_response(self, data):
return Response(OrderedDict([
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('current', self.get_current_link()),
('results', data)
]))
def get_current_link(self):
"""
Return a link to the current position.
- self.cursor set in the paginate_queryset() method.
To return only the query parameter in this field, use:
- return request.query_params.get(self.cursor_query_param, None)
"""
if self.cursor:
return self.encode_cursor(self.cursor)
else:
# cursor will be None on the first call
return None
def get_paginated_response_schema(self, schema):
new_schema = super().get_paginated_response_schema(schema)
new_schema["properties"]["current"] = {
"type": "string",
"nullable": True
}
return new_schema
class MyApiView(APIView):
pagination_class = FancyCursorPagination
这真的没有道理。当前页面是结果列表。光标会在那里做什么?@DanielRoseman移动下一个光标并返回到上一个光标会给你“当前光标”。这是保存当前位置的一种方法。这与获取最新的结果列表不同。但这不只是当前的URL吗?@DanielRoseman没有。也许我没有正确解释。这里有一个例子。e、 在当前的URL中,你会看到5篇文章[1,2,3,4,5]。一周后,又有100多个帖子。当前URL将为您提供[101、102、103、104、105],但您希望从上次中断的位置继续。很抱歉,我仍然不明白。为什么当前的URL会给你101,。。。而不是1,…?这真的没有意义。当前页面是结果列表。光标会在那里做什么?@DanielRoseman移动下一个光标并返回到上一个光标会给你“当前光标”。这是保存当前位置的一种方法。这与获取最新的结果列表不同。但这不只是当前的URL吗?@DanielRoseman没有。也许我没有正确解释。这里有一个例子。e、 在当前的URL中,你会看到5篇文章[1,2,3,4,5]。一周后,又有100多个帖子。当前URL将为您提供[101、102、103、104、105],但您希望从上次中断的位置继续。很抱歉,我仍然不明白。为什么当前的URL会给你101,。。。而不是1,…self.encode\u cusor(self.cursor)
将抛出错误,因为它没有偏移量。如果给它一个0偏移量,它将给你一个空光标。你可以这样做,但有更多的边缘案件处理<代码>光标(偏移量=0,反向=True,位置=self.\u从实例(self.page[-1],self.ordering)中获取位置)
啊,我得到了。我没有解释第一个请求(没有给出游标)。在这种情况下,self.cusor
将为无。我将编辑该问题,但它仍然未经测试:Dself.encode\u cusor(self.cursor)
将抛出错误,因为它没有偏移量。如果给它一个0偏移量,它将给你一个空光标。你可以这样做,但有更多的边缘案件处理<代码>光标(偏移量=0,反向=True,位置=self.\u从实例(self.page[-1],self.ordering)中获取位置)
啊,我得到了。我没有解释第一个请求(没有给出游标)。在这种情况下,self.cusor
将为无。我会编辑这个问题,但它仍然没有经过测试:D