Can we use LimitOffsetPagination with viewsets.ViewSet in Django Rest Framework.
settings.py:
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.TokenAuthentication",
],
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.IsAuthenticated",
],
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
"PAGE_SIZE": 100,
}
Below is how my list view looks:
class UserViewSet(viewsets.ViewSet):
pagination_class = LimitOffsetPagination
def get_permissions(self):
"""
Instantiates and returns the list of permissions that this view requires.
"""
permission_classes = [IsAuthenticated]
if self.action == "list" or self.action == "retrieve":
permission_classes.append(UserReadPermission)
elif self.action == "create" or self.action == "update":
permission_classes.append(UserWritePermission)
elif self.action == "destroy":
permission_classes.append(UserDeletePermission)
return [permission() for permission in permission_classes]
def list(self, request):
serializer = UserIndexSerializer(data=request.query_params)
if serializer.is_valid():
users = User.objects.all()
serializer = UserGetSerializer(users, many=True)
response = {"users": serializer.data, "total": len(serializer.data)}
return AppResponse.success("User list found.", response)
return AppResponse.error(serializer.errors, None, http_error_code=400)
def create(self, request):
pass
def retrieve(self, request, pk=None):
pass
def update(self, request, pk=None):
pass
def partial_update(self, request, pk=None):
pass
def destroy(self, request, pk=None):
pass
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…