"django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet" 尝试将数据加载到我的模型中时

"django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet" when trying to load data into my model

我正在用 Django 开发应用程序。

我想在我的模型中加载数据,即 glossary_entry,但数据存储在 xlsx 文件中,即 dati_prova.xlsx

为了实现这一点,我开发了以下脚本:

import pandas as pd
from django.conf import settings

settings.configure()

from myapp.models import glossary_entry #this is line 7
 
path=r"mypath\dati_prova.xlsx"
 
with open(path) as f:
        reader = pd.read_excel(f)
        next(reader, None)  # skip the headers

        for row in reader:
                _, created = glossary_entry.objects.get_or_create(
                Lemma = row[0],
                Acronym = row[1],
                Definizione = row[2],
                )
            # creates a tuple of the new object or
            # current object and a boolean of if it was created

但是当我从 Anaconda 提示 运行 它时,我得到

File "load_glossary.py", line 7, in module ...

raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

有什么问题?

请注意:

我的应用程序运行很好,只是上传数据脚本失败。

请注意:

我复制粘贴

from django.conf import settings

settings.configure()

来自堆栈溢出答案,因为我收到错误:

django.core.exceptions.ImproperlyConfigured: Requested setting USE_TZ, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

但我没有经验,也不明白错误是什么。

更新

我在 https://groups.google.com/forum/#!topic/django-users/bF_lRbzzguA 上看到它可能是

The problem is that one of your applications imports models in its top-level init.py. This is not supported; for an explanation, you can read https://docs.djangoproject.com/en/1.9/ref/applications/#how-applications-are-loaded

更新

我将文件更改如下:

import pandas as pd

from django.conf import settings
settings.configure()

import django
django.setup() 


from myapp.models import mymodel
 
path=r"mypath\dati_prova.xlsx"
 
with open(path) as f:
        reader = pd.read_excel(f)
        next(reader, None)  # skip the headers

现在我得到:

RuntimeError: Model class myapp.models.mymodel doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

但这不是真的,因为在 settings.py 中我写了我的应用程序名称,项目 运行 没问题。只是脚本不起作用... 就像 python 无法读取我的 settings.py 。 有什么问题? 也许是在读另一套

更新

按照此处的建议
我已将脚本 load_glossary.py 移至

myapp>management>commands


将我的 xlsx 文件复制到 csv 文件中
并更新代码如下:
# myapp/management/commands/load_glossary.py

from django.core.management.base import BaseCommand, CommandError
import csv

class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('csv_file', nargs='+', type=str)

    def handle(self, *args, **options):
        for csv_file in options['csv_file']:
            dataReader = csv.reader(open(csv_file), delimiter=',', quotechar='"')
            for row in dataReader:
                
                Lemma=row[0],
                Acronym=row[1],
                Definition=row[2],
                
                
                # etc...
                self.stdout.write(
                    'Created glossary entry'
                
                )

我正在通过在 anaconda 提示符中输入来享用午餐

python ./manage.py load_glossary csv_file "mypath\dati_prova.csv"

但后来我得到

line 20, in handle dataReader = csv.reader(open(csv_file), delimiter=',', quotechar='"') FileNotFoundError: [Errno 2] No such file or directory: 'csv_file'

这次怎么了?

我用这个替换解决了问题:

import pandas as pd
from django.conf import settings

settings.configure()

from myapp.models import glossary_entry #this is line 7

path=r"mypath\dati_prova.xlsx"

with open(path) as f:
        reader = pd.read_excel(f)
        next(reader, None)  # skip the headers

        for row in reader:
                _, created = glossary_entry.objects.get_or_create(
                Lemma = row[0],
                Acronym = row[1],
                Definizione = row[2],
                )
            # creates a tuple of the new object or
            # current object and a boolean of if it was created

有了这个:

import pandas as pd
from myapp.models import glossary_entry

def pour_entire_entry_model():

    elements = glossary_entry.objects.all() 

    for element in elements:

        entry = acquired_terminology.objects.create()

        entry.Lemma = element.Lemma
        entry.Acronym = element.Acronym
        entry.Definizione = element.Definizione 

            # creates a tuple of the new object or
            # current object and a boolean of if it was created