我正在尝试为其余框架打开 redis 缓存。
我的配置似乎是正确的。 当我运行时,它会报告错误。如下图所示:
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesrest_frameworksettings.py", line 177, in import_from_string
return import_string(val)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangoutilsmodule_loading.py", line 17, in import_string
module = import_module(module_path)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libimportlib__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:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesrest_framework_extensionsutils.py", line 6, in <module>
from rest_framework_extensions.key_constructor.constructors import (
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesrest_framework_extensionskey_constructorconstructors.py", line 4, in <module>
from rest_framework_extensions.key_constructor import bits
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesrest_framework_extensionskey_constructorbits.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:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangodbmodelssqldatastructures.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libthreading.py", line 932, in _bootstrap_inner
self.run()
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libthreading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangoutilsautoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangocoremanagementcommandsrunserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangocoremanagementbase.py", line 392, in check
all_issues = checks.run_checks(
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangocorechecksregistry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangocorechecksurls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangocorechecksurls.py", line 23, in check_resolver
return check_method()
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangourlsresolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangoutilsfunctional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangourlsresolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangoutilsfunctional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangourlsresolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libimportlib__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_项目相关PythonWebTakeMeHomeTakeMeHomeurls.py", line 20, in <module>
from Website import views as views2
File "D:C_项目相关PythonWebTakeMeHomeWebsiteviews.py", line 155, in <module>
class Cache_test(APIView,):
File "D:C_项目相关PythonWebTakeMeHomeWebsiteviews.py", line 158, in Cache_test
@cache_response(timeout=60)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesrest_framework_extensionscachedecorators.py", line 38, in __init__
self.key_func = extensions_api_settings.DEFAULT_CACHE_KEY_FUNC
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesrest_frameworksettings.py", line 220, in __getattr__
val = perform_import(val, attr)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesrest_frameworksettings.py", line 166, in perform_import
return import_from_string(val, setting_name)
File "C:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesrest_frameworksettings.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:UsersHUANGAppDataLocalProgramsPythonPython38libsite-packagesdjangodbmodelssqldatastructures.py).
**导入错误:无法导入 API 设置"DEFAULT_CACHE_KEY_FUNC"的"rest_framework_extensions.utils.default_cache_key_func"。
ImportError: 无法从 'django.db.models.sql.datastructures' (C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\datastructures.py)导入名称 'EmptyResultSet'。
令人沮丧的是,我是初学者和菜鸟,我无法修复它。 而我在网上没有找到答案,我只能试着来这里寻求你的帮助
版本:
- 姜戈:3.2.10
- DRF : 3.11
- Redis: 3.0.2 (Windows Release)
我的设置:
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)
这个特殊的错误是由drf-extensions
尚未与Django 3.1+兼容引起的。(drf-extensions
是应用的依赖项之一;另外,您说您正在使用 3.2.10,但 3.2 仍在开发中;截至撰写本文时最新发布的版本是 3.1.1)我发现 GitHub 上有一个drf-extensions
项目来解决这个问题,但这还没有找到发布的方式。
您的一些选择:
- 检查是否确实需要依赖项,如果没有,请将其卸载。根据你的缓存需求,简单地使用 Django 的内置缓存框架可能就足够了。
- 将修复程序合并到我链接到该依赖项安装中的提交中(即在本地修补您的库)。
- 同时将你的 Django 安装降级到 3.0 版(作为最后的手段)。