嘗試重新定義paginator
屬性,如果有用戶查詢參數,指定您的自定義分頁類self._paginator
@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:
user = self.request.query_params.get('user', None)
if user is not None:
self._paginator = customPaginationClass()
else:
self._paginator = self.pagination_class()
return self._paginator
最終
class BlockViewSet(viewsets.ModelViewSet):
pagination_class = defaultPaginationClass
@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:
user = self.request.query_params.get('user', None)
if user is not None:
self._paginator = customPaginationClass()
else:
self._paginator = self.pagination_class()
return self._paginator
def get_queryset(self):
queryset = Block.objects.all()
user = self.request.query_params.get('user', None)
if user is no None:
queryset = queryset.filter(user=user)
return queryset
謝謝。像魅力一樣工作 – seibeki