gpt4 book ai didi

django - 在序列化程序 Django 中使用分页

转载 作者:行者123 更新时间:2023-12-02 15:19:37 26 4
gpt4 key购买 nike

我正在开发一个 Django 项目,我试图从 Product 表中获取所有 update_ts 字段大于 product_sync_ts 的产品一次从服务器获取,但由于产品数量很大,这需要大量时间来获取所有数据。我还将当前时间戳与数据一起返回。现在我想对结果进行分页。

我以前的代码:

Views.py

@api_view(['GET'])
def productlist(request, format=None):
product_sync_ts = request.GET.get(
'product_sync_ts', None)
if product_sync_ts:
product =Product.objects.filter(
update_ts__gt=product_sync_ts)
)
serializer = SyncedProductSerializer(
instance={'products': product})
return response(serializer.data)
else:
content = {'details': "Bad Request"}
raise APIException400(request, content)

serializer.py

class SyncedProductSerializer(serializers.Serializer):
product_sync_ts = serializers.SerializerMethodField(
method_name='get_current_ts'
)
products = ProductSerializer(many=True, read_only=True)

class Meta:
fields = ('products', 'product_sync_ts')

def get_current_ts(self, obj):
product_sync_ts = datetime.utcnow().isoformat(' ')
return product_sync_ts




class ProductSerializer(serializers.ModelSerializer):
image_url = serializers.SerializerMethodField(
method_name='change_image_url'
)

class Meta:
model = Product
fields = (
'id',
'name',
'unit',
'price',
'image_url'
)

def change_image_url(self, obj):
return ''.join([
CDN_URL, str(obj.image_url)
]) if str(obj.image_url) else None

我是如何尝试修改它的分页的:

Views.py

class PaginatedProductList(APIView):

def get(self, request, format=None):
product_sync_ts = request.GET.get(
'product_sync_ts', None)
if product_sync_ts:
product = GrProduct.objects.filter(
update_ts__gt=product_sync_ts
)
paginator = Paginator(product, 1000)
page = request.GET.get('page')
try:
product = paginator.page(page)
except PageNotAnInteger:
product = paginator.page(1)
except EmptyPage:
product = paginator.page(paginator.num_pages)

serializer = SyncedProductSerializer(
instance={'products': product})
return Response(serializer.data)
else:
content = {'details': "Bad Request"}
raise APIException400(request, content)

这工作正常,我得到分页结果,但问题是我无法将下一页和上一页信息发送给客户端。客户端无法知道有效(非空)页数是多少。

有人可以建议我如何修改我的代码才能做到这一点。

最佳答案

这是一个如何使用 APIView 类和 PageNumberPagination 分页类对结果进行分页的示例:

from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination

class ProductList(APIView, PageNumberPagination):
# number of items per page by default
page_size = 1000
# max number of items per page
max_page_size = 1000

def get_queryset(self):
product_sync_ts = self.request.GET.get('product_sync_ts', None)
if product_sync_ts:
products = GrProduct.objects.filter(update_ts__gt=product_sync_ts)
return self.paginate_queryset(products, self.request)

raise APIException400(request, {'details': "Bad Request"})

def get(self, request):
products = self.get_queryset()
serializer = SyncedProductSerializer(instance={'products': products})
return self.get_paginated_response(serializer.data)

阅读DRF - PageNumberPagination配置您的分页

关于django - 在序列化程序 Django 中使用分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290029/

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