2015-09-14 28 views
1

假設我有一個觀點:如何檢測用戶是否在Django REST(分頁)的最後一頁上?

class FooPagination(PageNumberPagination): 
    page_size = 10 

class FooView(ListAPIView): 
    serializer_class = FooSerializer 
    pagination_class = FooPagination 
    def get_queryset(self): 
     if self.paginator.? 
      # do something special 

如何檢測,如果用戶在Django的REST(分頁)的最後一頁上?

更新如果我嘗試:

print(self.paginator.get_next_link()) 

我得到:

Exception Type: AttributeError 
Exception Value: 'FooPagination' object has no attribute 'page' 
Exception Location: C:\Users\Daniel\venvs\brook\lib\site-packages\rest_framework\pagination.py in get_next_link, line 297 

我猜我想是不可能這樣的分頁程序不知道這是否是對直到查詢集被指定爲止的最後一頁。種雞和雞蛋的情況。

+0

嘗試使用has_next(),它會告訴你,一個分頁程序有可用與否 –

+0

self.paginator.has_next()下頁給出了異常類型:AttributeError異常,異常值:「FooPagination '對象沒有屬性'has_next'。 – tyebillion

+0

請點擊此鏈接http://www.django-rest-framework.org/api-guide/pagination/ –

回答

1

對於self.paginator.get_next_link()工作,你必須使用self.paginate_queryset(self.queryset)。通常,這是在視圖內部使用的方法,如list()而不是get_queryset()(兩者都可以工作!)。

然後,使用

if not self.paginator.get_next_link(): 
    # You're in last page 
    # your code goes here 
相關問題