[英]Django Rest Framework pagination on GenericViewSet
I have the following GenericViewSet, I am trying to achieve pagination for the viewset, This is my viewset 我有以下GenericViewSet,我正在尝试实现视图集的分页,这是我的视图集
class UserAccountViewSet(viewsets.GenericViewSet,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin):
queryset = UserAccount.objects.all()
lookup_field = 'username'
lookup_url_kwarg = "username"
serializer_class = UserAccountSerializer
page_size = 25
page_size_query_param = 'page_size'
max_page_size = 1000
def list(self, request):
queryset = self.queryset
if request.GET.dict():
return Response(status=status.HTTP_501_NOT_IMPLEMENTED)
serializer = UserListSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, **kwargs):
pass
def create(self, request, *args, **kwargs):
pass
def update(self, request, *args, **kwargs):
pass
def destroy(self, request, *args, **kwargs):
pass
this is my configuration, 这是我的配置,
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
),
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
),
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '100/day'
}
}
It is not getting paginated, how can I make pagination work with DRF? 它没有分页,如何使用DRF进行分页?
thank you. 谢谢。
Since you are overriding list()
method and not returning a paginated response, you are not getting paginated response in your API. 由于您要覆盖
list()
方法且未返回分页响应,因此您的API不会获得分页响应。 Instead, you should call super()
in list()
method as DRF's list()
method itself returns a paginated response for generic views or viewsets. 相反, 您应该在
list()
方法中调用super()
,因为DRF的list()
方法本身会为通用视图或视图集返回分页响应。
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.如果您使用的是常规
APIView
,则需要自己调用分页API,以确保您返回分页响应。
class UserAccountViewSet(viewsets.GenericViewSet,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin):
def list(self, request, *args, **kwargs):
if request.GET.dict():
return Response(status=status.HTTP_501_NOT_IMPLEMENTED)
# call 'super()' to get the paginated response
return super(UserAccountViewSet, self).list(request, *args, **kwargs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.