无法在用户模型中导入restframework.generics

bjp0bcyl  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(424)

在我们的代码库中,如果我们 import rest_framework.generics 我们的用户类(即我们的 BaseUserManager ),我们得到的错误如下所示:

File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 178, in get_model
    return self.models[model_name.lower()]
KeyError: 'acmeuser'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 157, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
  File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 211, in get_model
    return app_config.get_model(model_name, require_ready=require_ready)
  File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 180, in get_model
    raise LookupError(
LookupError: App 'acme_api' doesn't have a 'AcmeUser' model.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/topher/colony/platform/acme-api/manage.py", line 46, in <module>
    main()
  File "/Users/topher/colony/platform/acme-api/manage.py", line 38, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python@3.9/3.9.4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): o385127.ingest.sentry.io:443
  File "/Users/topher/colony/platform/acme-api/acme_api/models.py", line 1, in <module>
    from .model.acme_user import AcmeUser
  File "/Users/topher/colony/platform/acme-api/acme_api/model/acme_user.py", line 20, in <module>
    import rest_framework.generics
  File "/usr/local/lib/python3.9/site-packages/rest_framework/generics.py", line 9, in <module>
    from rest_framework import mixins, views
  File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 17, in <module>
    from rest_framework.schemas import DefaultSchema
  File "/usr/local/lib/python3.9/site-packages/rest_framework/schemas/__init__.py", line 33, in <module>
    authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
  File "/usr/local/lib/python3.9/site-packages/rest_framework/settings.py", line 220, in __getattr__
    val = perform_import(val, attr)
  File "/usr/local/lib/python3.9/site-packages/rest_framework/settings.py", line 168, in perform_import
    return [import_from_string(item, setting_name) for item in val]
  File "/usr/local/lib/python3.9/site-packages/rest_framework/settings.py", line 168, in <listcomp>
    return [import_from_string(item, setting_name) for item in val]
  File "/usr/local/lib/python3.9/site-packages/rest_framework/settings.py", line 177, in import_from_string
    return import_string(val)
  File "/usr/local/lib/python3.9/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "/usr/local/Cellar/python@3.9/3.9.4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/topher/Library/Python/3.9/lib/python/site-packages/rest_framework_simplejwt/authentication.py", line 6, in <module>
    from .state import User
  File "/Users/topher/Library/Python/3.9/lib/python/site-packages/rest_framework_simplejwt/state.py", line 6, in <module>
    User = get_user_model()
  File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 161, in get_user_model
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'acme_api.AcmeUser' that has not been installed
Sentry is attempting to send 0 pending error messages
Waiting up to 2 seconds
Press Ctrl-C to quit

对于这个稍微模糊的错误消息,您可以假设 settings.py 包含以下行: AUTH_USER_MODEL = 'acme_api.AcmeUser' 目前,我们通过将import语句放在方法定义中来“解决”这个问题,但似乎必须有更好的方法来实现这一点。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题