将Django中模型导入到Django服务中,core.exceptions.配置不正确:请求设置INSTALLED_APPS,但未配置设置

nfeuvbwi  于 2023-05-01  发布在  Go
关注(0)|答案(1)|浏览(140)

我正面临着以下问题。
在我的Django项目中,我创建了一个服务。py文件在“resume”app目录中,我想在其中放置一些项目业务逻辑。
以下是项目结构:

在此文件(services.py)我导入模型:

# services.py

from models import PersonalData

def get_personal_data():
    list_data = PersonalData.objects.all()
    return list_data

然后我得到以下异常:

django.core.exceptions.ImproperlyConfigured:请求设置INSTALLED_APPS,但未配置设置。必须定义环境变量DJANGO_SETTINGS_MODULE或调用设置。configure(),然后访问设置。

下面是整个Traceback:

Traceback (most recent call last):
  File "D:\python_projects\my_site\mysite\resume\services.py", line 9, in <module>
    from mysite.resume.models import PersonalData
  File "D:\python_projects\my_site\mysite\resume\models.py", line 4, in <module>
    class PersonalData(models.Model):
  File "D:\virtualenv\mysite\Lib\site-packages\django\db\models\base.py", line 129, in __new__
    app_config = apps.get_containing_app_config(module)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\virtualenv\mysite\Lib\site-packages\django\apps\registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "D:\virtualenv\mysite\Lib\site-packages\django\apps\registry.py", line 137, in check_apps_ready
    settings.INSTALLED_APPS
  File "D:\virtualenv\mysite\Lib\site-packages\django\conf\__init__.py", line 102, in __getattr__
    self._setup(name)
  File "D:\virtualenv\mysite\Lib\site-packages\django\conf\__init__.py", line 82, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Process finished with exit code 1

www.example中我的INSTALLED_APPSwww.example.com 有这个app

INSTALLED_APPS = [
    'resume.apps.ResumeConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

我发现的一个解决方案是调用django。setup()并将路径传递到settings。py。之后我在services中写了以下内容。py

# services.py

import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_site.settings')
django.setup()

from models import PersonalData

def get_personal_data():
    list_data = PersonalData.objects.all()
    return list_data

之后,我又得到了一个例外:

ModuleNotFoundError:没有名为“my_site”的模块

下面是整个Traceback:

Traceback (most recent call last):
  File "D:\python_projects\my_site\mysite\resume\services.py", line 6, in <module>
    django.setup()
  File "D:\virtualenv\mysite\Lib\site-packages\django\__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
                      ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\virtualenv\mysite\Lib\site-packages\django\conf\__init__.py", line 102, in __getattr__
    self._setup(name)
  File "D:\virtualenv\mysite\Lib\site-packages\django\conf\__init__.py", line 89, in _setup
    self._wrapped = Settings(settings_module)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\virtualenv\mysite\Lib\site-packages\django\conf\__init__.py", line 217, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alexa\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1128, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'my_site'

Process finished with exit code 1

我不明白为什么它找不到my_site模块,如果它存在。在manage中设置了类似的环境变量。py

# manage.py

def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_site.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

if __name__ == '__main__':
    main()

我还尝试在cmd中设置环境变量:
D:\python_projects\my_site〉set SJANGO_SETTINGS_MODULE = my_site.设置
但也没帮上忙。我想我尝试了所有的答案是堆栈溢出,但没有工作
请帮帮忙,我该怎么办,出了什么事?

eh57zj3b

eh57zj3b1#

my_site是项目,而你需要处理的应用程序resume是你的应用程序。
你也有模型导入的问题。

from .models import PersonalData

并删除其他不必要的代码到django。DJANGO_SETTINGS_MODULE的setup()和环境变量。
另一个我能看到的错误是项目设置上的错字错误。您的项目名称是mysite而不是my_site

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

相关问题