I'm trying to open the redis cache for the rest framework.
My configuration seems to be correct. When I run, it reports an error. As shown below:
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: 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).**
It's frustrating that I'm a beginner and a rookie, I can't fix it. And I didn't find the answer on the Internet, I can only try to come here and ask for your help
version:
- Django: 3.2.10
- drf : 3.11
- redis: 3.0.2 (windows release)
my settings:
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)
This particular error is caused by
drf-extensions
not being compatible yet with Django 3.1+. (drf-extensions
is one of the dependencies of your app; also, you say you're using 3.2.10, but 3.2 is still under development; the latest released version as of writing is 3.1.1) I've found a commit to thedrf-extensions
project on GitHub solving this issue, but that has not found its way yet to a release.Some of your options: