所以我之前发布了关于Django的一些问题,但那是不可能解决的。之后我改变了方法。这是我目前面临的问题。每当我尝试运行此脚本时,它都会抛出此错误...
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/dummy/base.py", line 21, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
这似乎发生在我将导入导出并将db脚本的csv版本导出到主项目文件夹而不是app文件夹之后。这就是代码目前的样子......
# exports db as a csv file
import sys, os
#sys.path.append(os.path.expanduser('~/Documents/swen261/masterProject/HealthNet'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'HealthNet.settings'
import django
from django.conf import settings
settings.configure()
django.setup()
print(sys.path)
from django.db import models
from main_site.models import *
print("Starting DB export script...\n")
print("Creating csv file 'csv_output.csv'...\n")
with open('csv_output.csv', 'w+') as file:
print("Writing out Patients...\n")
for p in Patient.objects.all():
file.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,\n"
% ("Patient", p.user.username, p.user.password, p.user.first_name, p.user.last_name, p.hospital.name, p.insurance_provider, p.insurance_id, "nil"))
print("Writing out Doctors...\n")
for d in Doctor.objects.all():
file.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,\n"
% ("Doctor", d.user.username, d.user.password, d.user.first_name, d.user.last_name, d.hospital.name, "nil","nil","nil"))
print("Writing out Nurses...\n")
for n in Nurse.objects.all():
file.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,\n"
% ("Nurse", n.user.username, n.user.password, n.user.first_name, n.user.last_name, n.hospital.name, "nil", "nil", "nil"))
print("Writing out Hospital Admins...\n")
for ha in HospitalAdmin.objects.all():
file.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,\n"
% ("HospitalAdmin", ha.user.username, ha.user.password, ha.user.first_name, ha.user.last_name, ha.hospital.name, "nil", "nil", "nil"))
print("Finished creating csv.\n")
这是settings.py
的相关部分# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
这是目录结构......
有没有人知道如何阻止这种情况发生。该脚本到达第一个Patient.objects.all(),然后抛出异常。
答案 0 :(得分:2)
from django.conf import settings
settings.configure()
使用默认设置(而不是您的应用设置)配置Django。
from HealthNet import settings as my_settings
from django.conf import settings
settings.configure(default_settings=my_settings)
删除以下行:
import django
from django.conf import settings
settings.configure()
django.setup()
Django使用DJANGO_SETTINGS_MODULE
变量进行自动配置。