无论我做什么,Django投掷错误配置错误

时间:2015-12-06 15:18:09

标签: python django csv import

我一直在试图弄清楚如何以某种格式将我的数据库导出为csv文件(将数据转储到JSON并转换为csv将无法工作或将花费太长时间)。目前我有一个脚本,它只迭代每个对象并将相关信息写出到文件中。然而,由于一些奇怪的原因,Django抱怨投掷:

 Traceback (most recent call last):
 File "csv_export.py", line 4, in <module>
 from models import Patient, Nurse, Doctor, HospitalAdmin
 File "/home/ian/Documents/swen261/master_project/HealthNet/main_site/models.py", line 2, in <module>
from django.contrib.auth.models import User
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/__init__.py", line 7, in <module>
from django.middleware.csrf import rotate_token
  File "/usr/local/lib/python2.7/dist-packages/django/middleware/csrf.py", line 14, in <module>
from django.utils.cache import patch_vary_headers
  File "/usr/local/lib/python2.7/dist-packages/django/utils/cache.py", line 26, in <module>
from django.core.cache import caches
  File "/usr/local/lib/python2.7/dist-packages/django/core/cache/__init__.py", line 34, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
 django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

这是脚本:

# exports db as a csv file

import sys, os, django
sys.path.append('~/Documents/swen261/masterProject/HealthNet/main_site/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'main_site.settings'
django.setup()
from models import Patient, Nurse, Doctor, HospitalAdmin

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")
# formatting got a little screwed here, the rest below this should be indented.
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")

The directory structure. And the template thing is a bug with pycharm.

我不确定为什么会这样做或如何修复它。问题似乎在于导入,即使我可以合法地在视图文件中执行它。其他每个有同样问题的人似乎都采用了不同的方式,我根据他们的答案无法弄明白。

2 个答案:

答案 0 :(得分:0)

在模块顶部添加此项(为项目调整正确的路径以及&#39;设置&#39;)

import sys, os
sys.path.append('/path/to/your/django/app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.conf import settings
django.setup()

答案 1 :(得分:0)

改变这个:

import sys, os, django
sys.path.append('~/Documents/swen261/masterProject/HealthNet/main_site/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'main_site.settings'

到此:

import sys, os
sys.path.append(os.path.expanduser('~/Documents/swen261/masterProject/HealthNet')))
os.environ['DJANGO_SETTINGS_MODULE'] = 'HealthNet.settings'
import django

DJANGO_SETTINGS_MODULE env var必须是可导入的路径。这应该在导入django之前完成。