django - 当我尝试导入csv文件时出现“AppRegistryNotReady”消息

时间:2016-01-08 16:02:08

标签: python django csv

我想将导入csv文件(test.csv)发送到数据库(Store model

这是我的代码。

csv_upload.py

# encoding: utf-8

csv_filepathname = "PATH"
your_djangoproject_home = "PATH"

import sys, os
sys.path.append(your_djangoproject_home)
os.environ['DJANGO_SETTINGS_MODULE'] = "lotterymap.settings"

from store.models import Store

import csv

with open(csv_filepathname) as f:
    reader = csv.reader(f)
    for row in reader:
        _, created = Store.objects.get_or_create(
            name=row[0],
            address=row[1],
        )

我的路径

lotterymap
  |> lotterymap
  |> store
  | csv_upload.py
  | test.csv

如果我输入python csv_upload.py,则django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

我该怎么办?

1 个答案:

答案 0 :(得分:3)

从Django 1.8开始,您应该explicitly call django.setup()

import django
from django.conf import settings
settings.configure()
django.setup()
# Now you can use your models
from store.models import Store