gpt4 book ai didi

python - 分页在 DRF APIView 中不起作用

转载 作者:行者123 更新时间:2023-12-04 16:10:20 24 4
gpt4 key购买 nike

我正在使用 APIView用于获取和发布项目。
我想使用 Django Rest Framework 为我的 API 实现分页,但它不起作用。

我想每页显示 10 个项目,但是当我这样做时 api/v1/items?page=1 , 我得到了所有的元素,如果我只是这样做的话 api/v1/items我得到一个空列表。

这是我所做的:

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

class ItemsAPIView(APIView):
permission_classes = (permissions.IsAuthenticated,)

def get(self, request, format=None):
"""
Return a list of all items of this user.
"""
reply = {}
page = request.GET.get('page')
print ('page is', page)
try:
products = BaseItem.objects.owned_items().filter(owner=request.user)
reply['data'] = OwnedItemSerializer(products, many=True).data

items = BaseItem.objects.filter(owner=request.user)
paginator = Paginator(items, 1)
items_with_pagination = paginator.page(page)
if page is not None:
reply['data'].extend(ItemSerializer(items_with_pagination, many=True).data)
reply['data'].extend(ItemSerializer(items, many=True).data)

最佳答案

非通用 View 和 View 集 do not have pagination by default 如 django rest 框架文档中所述:

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. See the source code for the mixins.ListModelMixin and generics.GenericAPIView classes for an example.



我已经编写了一个 full example on enabling pagination on non generic views 与如何实现这一点的代码:
class ItemsAPIView(APIView):
permission_classes = (permissions.IsAuthenticated,)
pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
serializer_class = MyNewUnifiedSerializerClass

def get(self, request):
user_items = BaseItem.objects.filter(
owner=request.user
).distinct()
page = self.paginate_queryset(user_items)

if page is not None:
serializer = self.serializer_class(page, many=True)
return self.get_paginated_response(serializer.data)

serializer = self.get_serializer(user_items, many=True)
return Response(serializer.data)

# Now add the pagination handlers taken from
# django-rest-framework/rest_framework/generics.py

@property
def paginator(self):
"""
The paginator instance associated with the view, or `None`.
"""
if not hasattr(self, '_paginator'):
if self.pagination_class is None:
self._paginator = None
else:
self._paginator = self.pagination_class()
return self._paginator

def paginate_queryset(self, queryset):
"""
Return a single page of results,
or `None` if pagination is disabled.
"""
if self.paginator is None:
return None
return self.paginator.paginate_queryset(
queryset,
self.request,
view=self
)

def get_paginated_response(self, data):
"""
Return a paginated style `Response` object
for the given output data.
"""
assert self.paginator is not None
return self.paginator.get_paginated_response(data)

有关更多详细信息,请查看 example link provided

关于python - 分页在 DRF APIView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43159208/

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