首页 > 解决方案 > 无法使用来自 drf-extensions 的 mixin

问题描述

我已经安装了 drf-extensions 并将其导入到我的视图中。之后,我开始低于异常,我无法确定其根本原因。有人可以解释一下,请问这里出了什么问题?

> python .\manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework\settings.py", line 177, in import_from_string
    return import_string(val)
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "C:\Users\gknath\AppData\Local\Programs\Python\Python38-32\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\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework_extensions\utils.py", line 6, in <module>
    from rest_framework_extensions.key_constructor.constructors import (
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\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\gknath\Desktop\python-django\django-models-rest\venv\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\gknath\Desktop\python-django\django-models-rest\venv\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\gknath\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\gknath\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = checks.run_checks(
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\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\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
    for pattern in self.url_patterns:
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\gknath\AppData\Local\Programs\Python\Python38-32\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\gknath\Desktop\python-django\testing_new_backend_model_modp\projects\urls.py", line 5, in <module>
    from .api import router
  File "C:\Users\gknath\Desktop\python-django\testing_new_backend_model_modp\projects\api.py", line 2, in <module>
    from api.views import FactorViewSet, ProjectViewSet, FactorViewSet
  File "C:\Users\gknath\Desktop\python-django\testing_new_backend_model_modp\api\views.py", line 3, in <module>
    from rest_framework_extensions.mixins import NestedViewSetMixin
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework_extensions\mixins.py", line 1, in <module>
    from rest_framework_extensions.cache.mixins import CacheResponseMixin
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework_extensions\cache\mixins.py", line 5, in <module>
    class BaseCacheResponseMixin:
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework_extensions\cache\mixins.py", line 8, in BaseCacheResponseMixin
    object_cache_key_func = extensions_api_settings.DEFAULT_OBJECT_CACHE_KEY_FUNC
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework\settings.py", line 225, in __getattr__
    val = perform_import(val, attr)
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework\settings.py", line 166, in perform_import
    return import_from_string(val, setting_name)
  File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\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_object_cache_key_func' for API setting 'DEFAULT_OBJECT_CACHE_KEY_FUNC'. ImportError: cannot import name 'EmptyResultSet' from 'django.db.models.sql.datastructures' (C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\db\models\sql\datastructures.py).

如果我禁用扩展导入,那么我可以正常运行。我正在使用三个模型并编写了序列化程序并尝试使用 drf_extensions 制作嵌套 API。

标签: pythondjango-modelsdjango-rest-frameworkdrf-extensions

解决方案


我猜这个问题与这个问题有关:https ://github.com/chibisov/drf-extensions/issues/294

对我来说临时修复是卸载和安装:

pip install -U git+git://github.com/chibisov/drf-extensions.git@8001a440c7322be26bbe2d16f3a334a8b0b5860b

推荐阅读