简体   繁体   English

Django-Rest-Framework:在Django settings.py中全局设置分页类

[英]Django-Rest-Framework: Globally set pagination class in django settings.py

I'm trying to make a default pagination on all api calls: 我正在尝试对所有api调用进行默认分页:

http://www.django-rest-framework.org/api-guide/pagination/#modifying-the-pagination-style http://www.django-rest-framework.org/api-guide/pagination/#modifying-the-pagination-style

And now I want to make my CustomPagination work globally: 现在,我想使CustomPagination在全球范围内工作:

class CustomPagination(PageNumberPagination):
    """
    自定义分页器
    """
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 1000

I want the register the class to the settings.py : 我想将类注册到settings.py

# =========== REST Framework ==============
REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'football.views.CustomPagination',
    'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',),
}

Still, it raised an error: 尽管如此,它还是引发了一个错误:

ImportError: Could not import 'football.views.CustomPagination' for API setting 'DEFAULT_PAGINATION_CLASS'. ImportError:无法为API设置“ DEFAULT_PAGINATION_CLASS”导入“ football.views.CustomPagination”。 AttributeError: module 'football.views' has no attribute 'CustomPagination'. AttributeError:模块“ football.views”没有属性“ CustomPagination”。

How can I work around it? 我该如何解决?

I encountered the same problem, and finally I fingered out it was because the module views.py was not correctly loaded because I didn't create the rest api folder by manage.py startapp and there is no item for it in INSTALLED_APPS of project's setting.py file. 我遇到了同样的问题,最后我指出这是因为未正确加载模块views.py ,因为我没有通过manage.py startapp创建rest api文件夹,并且在项目setting.py INSTALLED_APPS中没有任何项目setting.py文件。
I moved the CustomPagination paging class to views.py of my first app which was created by manage.py startapp then it worked. 我将CustomPagination分页类移到了第一个由manage.py startapp创建的应用程序的views.py ,然后工作了。

To debug, you can add following line to rest_framework/settings.py source code like this: 要进行调试,您可以将以下行添加到rest_framework / settings.py源代码中,如下所示:

module = import_module(module_path)  # Original code
if (setting_name == "DEFAULT_PAGINATION_CLASS"):  # Added code
    print(dir(module))  # Added code
return getattr(module, class_name)   # Original code

If AttributeError raised, it should be like: (only builtin attributes in the list) 如果引发AttributeError ,则应类似于:(仅列表中的内置属性)

# ./manage.py runserver 0:8000
Performing system checks...

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
Unhandled exception in thread started by .wrapper at 0x7fd60a265510>
Traceback (most recent call last):

If it works, CustomPagination should be listed in the list: 如果CustomPagination应该在列表中列出CustomPagination

# ./manage.py runserver 0:8000
Performing system checks...

['some-other-classes', 'PageNumberPagination', 'CustomPagination', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'models', 'random', 'reverse', 'settings']
System check identified no issues (0 silenced).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM