如何在django-rest-framework中将pagination_class用于自定义分页类 [英] How can I use pagination_class in django-rest-framework for my custom pagination class
问题描述
我的分页类
class ArticleListPagination(PageNumberPagination):
page_size = 2
page_size_query_param = 'page_size'
我的文章视图类
class Article(generics.GenericAPIView):
queryset = Articles.objects.all()
serializer_class = ArticlesSerializer
pagination_class = ArticleListPagination
def get(self, request):
queryset = self.get_queryset()
serializer = ArticlesSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
I能够使用以下自定义分页类
I am able to use custom pagination class using this
def get(self, request):
queryset = self.get_queryset()
page = ArticleListPagination()
new = page.paginate_queryset(queryset, request)
serializer = ArticlesSerializer(new, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
使用 ArticleListPagination
是否正确?如果我在课堂上说我的分页类别是 ArticleListPagination
,为什么它没有更改返回的查询集对象。
Is it the proper way to use ArticleListPagination
? If I have stated in my class that my pagination class is ArticleListPagination
, why it is not changing the return queryset object.
推荐答案
:
您可以扩展 rest_framework.mixins.ListModelMixin
直接,或者您实施 get
或 list
类似的方法。
for django_rest_framework 3.0.x (or below):
You can extends the rest_framework.mixins.ListModelMixin
directly, or you imlement the get
or list
method similar to that.
当然,还需要 generics.GenericAPIView
。
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
# get_paginaion_serializer will read your DEFAULT_PAGINATION_SERIALIZER_CLASS
# or view.pagination_serializer_class
# we will talk the two variable later
serializer = self.get_pagination_serializer(page)
else:
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
如果要配置为 全局,则可以在设置中进行配置。py
if you wanna config it "global", you can config in your settings.py
REST_FRAMEWORK = {
# ...
'DEFAULT_PAGINATION_SERIALIZER_CLASS': 'YourCustomPaginationSerializer',
# ...
}
如果您只是想设置为特定视图:
if you just wanna set to the specific view:
属性为 pagination_serializer_class
而不是 pagination_class
。
class MyView(generics.GenericAPIView):
pagination_serializer_class = YourCustomPaginationSerializerClass
用于django_rest_framework 3.1.x :
您可以扩展 rest_framework.mixins.ListModelMixin
直接,或者您实施类似的 get
方法。
You can extends the rest_framework.mixins.ListModelMixin
directly, or you imlement the get
method similar to that.
当然也需要 generics.GenericAPIView
。
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
如果要配置它是 全局,则您可以在您的设置中进行配置。py
if you wanna config it "global", you can config in your settings.py
REST_FRAMEWORK = {
# ...
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination'
# ...
}
如果您只是想设置为特定视图:
if you just wanna set to the specific view:
class MyView(generics.GenericAPIView):
pagination_class = YourCustomPaginationClass
这篇关于如何在django-rest-framework中将pagination_class用于自定义分页类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!