1

我正在尝试为其余框架打开 redis 缓存。

我的配置似乎是正确的。当我运行时,它会报告错误。如下图:</p>

Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework\settings.py", line 177, in import_from_string
    return import_string(val)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework_extensions\utils.py", line 6, in <module>
    from rest_framework_extensions.key_constructor.constructors import (
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework_extensions\key_constructor\constructors.py", line 4, in <module>
    from rest_framework_extensions.key_constructor import bits
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework_extensions\key_constructor\bits.py", line 3, in <module>
    from django.db.models.sql.datastructures import EmptyResultSet
ImportError: cannot import name 'EmptyResultSet' from 'django.db.models.sql.datastructures' (C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\datastructures.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = checks.run_checks(
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\urls\resolvers.py", line 408, in check
    for pattern in self.url_patterns:
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\C_项目相关\PythonWeb\TakeMeHome\TakeMeHome\urls.py", line 20, in <module>
    from Website import views as views2
  File "D:\C_项目相关\PythonWeb\TakeMeHome\Website\views.py", line 155, in <module>
    class Cache_test(APIView,):
  File "D:\C_项目相关\PythonWeb\TakeMeHome\Website\views.py", line 158, in Cache_test
    @cache_response(timeout=60)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework_extensions\cache\decorators.py", line 38, in __init__
    self.key_func = extensions_api_settings.DEFAULT_CACHE_KEY_FUNC
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework\settings.py", line 220, in __getattr__
    val = perform_import(val, attr)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework\settings.py", line 166, in perform_import
    return import_from_string(val, setting_name)
  File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework\settings.py", line 180, in import_from_string
    raise ImportError(msg)
ImportError: Could not import 'rest_framework_extensions.utils.default_cache_key_func' for API setting 'DEFAULT_CACHE_KEY_FUNC'. ImportError: cannot import name 'EmptyResultSet' from 'django.db.models.sql.datastructures' (C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\datastructures.py).

**ImportError:无法为 API 设置“DEFAULT_CACHE_KEY_FUNC”导入“rest_framework_extensions.utils.default_cache_key_func”。

ImportError:无法从“django.db.models.sql.datastructures”导入名称“EmptyResultSet”(C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\ sql\datastructures.py).**

令人沮丧的是我是初学者和菜鸟,我无法解决它。而且我在网上也没有找到答案,我只能试着来这里寻求你的帮助

版本:

  • 姜戈:3.2.10
  • 博士:3.11
  • redis: 3.0.2 (windows版本)</li>

我的设置:

CACHES = {
    'default':{
            'BACKEND': 'django_redis.cache.RedisCache',
            'LOCATION': 'redis://127.0.0.1:6379/0',## if redis server has passwd,
                    # "LOCATION": "password@redis://127. 0.0.1:6379",
            'OPTIONS': {
                'CLIENT_CLASS': 'django_redis.client.DefaultClient',
            }
        },
}

#
REST_FRAMEWORK_EXTENSIONS = {
    'DEFAULT_CACHE_KEY_FUNC':
      'rest_framework_extensions.utils.default_cache_key_func'
}

#views.py Like this:

from Users.serializers import UserAddrModelSerializer
from rest_framework_extensions.cache.decorators import cache_response
class Cache_test(APIView,):
    permission_classes = []
    throttle_classes = (AnonRateThrottle, UserRateThrottle)
    @cache_response(timeout=60)
    def get(self, request, *args, **kwargs):

        obj = UserAddress.objects.all()
        result = UserAddrModelSerializer(instance=obj, many=True).data
        return Response(result)
4

1 回答 1

0

此特定错误是由于drf-extensions与 Django 3.1+ 不兼容造成的。(drf-extensions是您的应用程序的依赖项之一;另外,您说您正在使用 3.2.10,但 3.2 仍在开发中;截至撰写本文时最新发布的版本是 3.1.1)我找到了对该项目的提交drf-extensions在 GitHub 上解决了这个问题,但还没有找到发布的方法。

您的一些选择:

  1. 检查你是否真的需要依赖,如果不需要,卸载它。根据您的缓存需求,只需使用 Django 的内置缓存框架就足够了。
  2. 将我链接到您安装该依赖项的提交中的修复程序合并(即在本地修补您的库)。
  3. 同时将您的 Django 安装降级到 3.0 版(作为最后的手段)。
于 2020-09-29T16:25:04.463 回答