从django中的csv导入数据时出错

时间:2017-05-13 22:16:02

标签: python django csv

我正在尝试将数据从csv导入django数据库。

models.py

class contactDetails(models.Model):
firstname = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
phonenumber = models.IntegerField()
email = models.CharField(max_length=100)

 def __str__(self):
    return self.firstname

save_form_data是项目名称,AppFormSaving是一个应用程序。

import.py

project_dir = 'K:\\PY\\save_form_data'
## project_dir = 'K:\\PY\\save_form_data\AppFormSaving'
sys.path.append(project_dir)

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django

django.setup()

from AppFormSaving.models import contactDetails

data = csv.reader(open('K:\\sample.csv'), delimiter=",")

for row in data:
    if row[0] != 'firstname':
        details = contactDetails()
        details.firstname = row[0]
        details.lastname = row[1]
        details.phonenumber = row[2]
        details.email = row[3]

        details.save()

当我运行import.py

    K:\PY\save_form_data>python import.py

我在下面收到错误,

 File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 55, in __geta
ttr__
    self._setup(name)
  File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 43, in _setup

    self._wrapped = Settings(settings_module)
  File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 99, in __init
__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked

ImportError: No module named 'settings'

我遵循了this教程。 我在这里缺少什么?

0 个答案:

没有答案