我在其中我試圖獲取所有具有update_ts
場比product_sync_ts
從時間服務器更大的Product
表產品中的Django項目工作,但由於產品數量較大此需要大量的時間來獲取所有數據。我還將當前的時間戳與數據一起返回。現在我想對結果進行分頁。在串行器使用分頁的Django
我以前的代碼:
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)
這是工作的罰款,我得到分頁結果,但問題是,我不能到下一個和以前的頁面信息發送到客戶端。客戶端無法知道什麼是有效(非空)頁數。
有人可以建議我如何修改我的代碼,以便這樣做。
感謝它像一個魅力:) –
也如何添加page_size和current_page_number隨着下一個和以前? –
我想自定義get_paginated_response方法,但我不確定我是否正確地執行該操作 –