如何从 .py 文件访问设置文件 - DRF

How to Access Settings file from .py file - DRF

这是我的项目结构。我正在尝试从 lookups.py

访问 settings.py 文件

我正在使用以下代码导入,

import os
import sys
import django

sys.path.append('/path/to/django/project')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'manageemployee.settings')
django.setup()
from manageemployee.apps.employee.hrmmongodb import DBMixin
from manageemployee.settings import EVENT_STORE_DICT

但是我收到以下错误,

ModuleNotFoundError: No module named 'manageemployee'

最好通过在需要的地方导入对象 django.conf.settings 来使用设置,而不是在尝试时导入设置文件本身,

示例:

from django.conf import settings

if settings.DEBUG:
    # Do Something

因此在您的用例中:

from django.conf import settings

EVENT_STORE_DICT = settings.EVENT_STORE_DICT 
# Do Something

请注意 django.conf.settings 不是一个模块——它是一个对象。所以 无法导入个人设置:

from django.conf.settings import DEBUG  # This won't work.

You can also find the detailed information in the Docs