Django Rest框架3.2.3 pagination不适用于generics.ListCreateAPIView [英] Django Rest Framework 3.2.3 pagination not working for generics.ListCreateAPIView

查看:519
本文介绍了Django Rest框架3.2.3 pagination不适用于generics.ListCreateAPIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说我应该在分类一般的List或ListCreateAPIView时免费得到分页,但没有发生任何分页的迹象。



这是我在设置中的...

  REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS':
'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE':25,
'MAX_PAGE_SIZE':50,
'TEST_REQUEST_DEFAULT_FORMAT' 'json',
'TEST_REQUEST_RENDERER_CLASSES':(...
),
'DEFAULT_FILTER_BACKENDS':(...

}

我的观点:

 类RequestList(generics.ListCreateAPIView):

#设置序列化的上下文
def get_serializer_context(self,* args,** kwargs):
context = {
'请求':self.request,
'view':self,
'format':self.format_kwarg,
'request_type_id':1}
return context

request_type_code_model_map = {
S:Request.objects.filter(request_t ype_id = 1,status ='open'),
...}

def get(self,request,request_type_code =S,format = None,* args,** kwargs )

queryset = self.request_type_code_model_map.get(
request_type_code,S)
serializer_class = RequestSerializer
serializer = serializer_class(
instance = queryset ,context = self.get_serializer_context(),
many = True)

返回响应(serializer.data)

任何帮助将不胜感激。我已经尝试创建一个自定义分页器类,在设置中设置各种配置选项,但是我没有做任何尝试分页。分页正在使用正在使用ViewSets的用户和组,但不是使用泛型的任何我的观点。任何想法,关于我失踪的线索或解决方案的线索都将不胜感激。

解决方案

你得到分页免费提供,不要覆盖调用它的部分。
您可以浏览列表源代码看看它是如何实现的,以及如何写你的 get 方法。


The docs say I should get pagination for free when subclassing a generic List or ListCreateAPIView but there is no sign of any pagination happening.

Here is what I have in settings...

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS':   
        'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 25,
    'MAX_PAGE_SIZE': 50,
    'TEST_REQUEST_DEFAULT_FORMAT': 'json',
    'TEST_REQUEST_RENDERER_CLASSES': (...
    ),
    'DEFAULT_FILTER_BACKENDS': (...
    )
}

My View:

class RequestList(generics.ListCreateAPIView):

    # set context for serializers
    def get_serializer_context(self, *args, **kwargs):
        context = {
            'request': self.request,
            'view': self,
            'format': self.format_kwarg,
            'request_type_id': 1}
    return context 

    request_type_code_model_map = {
        "S": Request.objects.filter(request_type_id=1, status='open'),
        ...}

    def get(self, request, request_type_code="S", format=None, *args,  **kwargs):

        queryset = self.request_type_code_model_map.get(
            request_type_code, "S")
        serializer_class = RequestSerializer
        serializer = serializer_class(
            instance=queryset, context=self.get_serializer_context(),
            many=True)

     return Response(serializer.data)

Any help would be much appreciated. I've tried creating a custom paginator class, setting various configuration options in settings, but nothing I do seems to make any attempt at paginating. Pagination is working for Users and Groups which are using ViewSets, but not any of my views using generics. Any ideas, a clue as to what I am missing, or a solution would be much appreciated.

解决方案

You get the pagination for free provided you don't override parts that call it. You can browse the list source to see how it's implemented and how you should write your get method.

这篇关于Django Rest框架3.2.3 pagination不适用于generics.ListCreateAPIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆