我想将导入csv文件(test.csv
)发送到数据库(Store model
)
这是我的代码。
# 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.
我该怎么办?
答案 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