gpt4 book ai didi

python - Django rest PageNumber 分页

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:55 25 4
gpt4 key购买 nike

我有一个 django rest 框架的简单分页类:

class StandardResultsSetPagination(PageNumberPagination):
page_size = 100
page_size_query_param = 'page_size'
max_page_size = 1000

# Create your views here.
class ResultSet(generics.ListAPIView):
queryset = Result.objects.all()
serializer_class = ResultSetSerializer
pagination_class = StandardResultsSetPagination

这个类当前返回一个带有 'prev''next' 键的字典,其中包含带有下一页和上一页的 URL。我想要实现的是 'prev''next' 只返回页码而不是整个 URL。

我怎样才能做到这一点?

最佳答案

您可以覆盖 PageNumberPagination 的 get_next_linkget_previous_link默认为:

   def get_next_link(self):
if not self.page.has_next():
return None
url = self.request.build_absolute_uri()
page_number = self.page.next_page_number()
return replace_query_param(url, self.page_query_param, page_number)

def get_previous_link(self):
if not self.page.has_previous():
return None
url = self.request.build_absolute_uri()
page_number = self.page.previous_page_number()
if page_number == 1:
return remove_query_param(url, self.page_query_param)
return replace_query_param(url, self.page_query_param, page_number)

你可以覆盖:

class StandardResultsSetPagination(PageNumberPagination):
page_size = 100
page_size_query_param = 'page_size'
max_page_size = 1000

def get_next_link(self):
if not self.page.has_next():
return None
page_number = self.page.next_page_number()
return page_number

def get_previous_link(self):
if not self.page.has_previous():
return None
page_number = self.page.previous_page_number()
return page_number

关于python - Django rest PageNumber 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43645889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com