我知道我不是第一个提出这个问题的人,但是我仍然找不到适合我情况的答案。
我有一个运行良好的Django环境,我知道我只有一个(1)settings.py
文件,而且我知道我的环境可以正确访问它。
我最近在项目中添加了一个新端点。我在urls.py
urlpatterns = [
...
url(PREFIX + r"^v1/alerts/debug$", alerts_views_v1.debug_trigger_alert_notification),
...
]
它连接到文件alerts/views_v1.py
中但不在任何特定类中的方法:
@api_view(["POST"])
def debug_trigger_alert_notification(request):
"""
Trigger an Alert. i.e. Create an Alertnotification.
This function is meant to be called by the scheduler service.
"""
workspace_slug = request.data.pop("workspace")
with in_database(workspace_slug, write=True):
serializer = AlertNotificationSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response()
当我向该URL发送请求时,出现以下500错误:
ImproperlyConfigured at /v1/alerts/debug
settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
在我的settings.py
文件中,我的DATABASES
变量似乎是正确的(尽管它是通过环岛方式构建的):
DEFAULT_DATABASES = {
"default": { # This DB is supposed to always have the latest version of the schema described by the Django Model
"ENGINE": "django.db.backends.postgresql",
"NAME": os.environ["REFERENCE_DB_NAME"],
"USER": os.environ["DB_USER"],
"PASSWORD": os.environ["DB_PASSWORD"],
"HOST": os.environ["DB_HOST"],
"PORT": os.environ["DB_PORT"],
}
}
# Retrieve all existing workspace databases:
try:
_existing_workspace_database_names = ExistingWorkspace(
db_user=DEFAULT_DATABASES["default"]["USER"],
db_host=DEFAULT_DATABASES["default"]["HOST"],
db_password=DEFAULT_DATABASES["default"]["PASSWORD"],
).list_existing_workspaces()
except Exception as e:
log.critical("settings.py: Error retrieving list of existing databases.")
raise e
else:
log.info("settings.py: Successfully retrieved list of existing databases.")
WORKSPACE_DATABASES = {
db_name_to_slug(existing_workspace_database_name): {
"ENGINE": "django.db.backends.postgresql",
"NAME": existing_workspace_database_name,
"USER": os.environ["DB_USER"],
"PASSWORD": os.environ["DB_PASSWORD"],
"HOST": os.environ["DB_HOST"],
"PORT": os.environ["DB_PORT"],
}
for existing_workspace_database_name in _existing_workspace_database_names
}
DATABASES = {**DEFAULT_DATABASES, **WORKSPACE_DATABASES}
DATABASE_ROUTERS = ["dynamic_db_router.DynamicDbRouter"]
我该怎么办?