“django.core.exceptions.AppRegistryNotReady:应用程序尚未加载"尝试将数据加载到我的模型中时 [英] "django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet" when trying to load data into my model

查看:26
本文介绍了“django.core.exceptions.AppRegistryNotReady:应用程序尚未加载"尝试将数据加载到我的模型中时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Django 中开发一个应用程序.

I am developing an app in Django.

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

I want to load data inside my model, that is glossary_entry, but the data is stored inside an xlsx file, that is dati_prova.xlsx.

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

In order to achieve this, I have developed the following script:

import pandas as pd
from django.conf import settings

settings.configure()

from myapp.models import glossary_entry #this is line 7

path=r"mypathdati_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 提示符运行它时,我得到

But when I run it from Anaconda prompt, I get

文件load_glossary.py",第 7 行,在模块中...

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

raise AppRegistryNotReady("应用程序尚未加载.")django.core.exceptions.AppRegistryNotReady:应用程序尚未加载.

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

有什么问题吗?

请注意:

我的应用运行良好,只是上传数据脚本失败.

My app runs fine, just the uploading-data script fails.

请注意:

我复制粘贴

from django.conf import settings

settings.configure()

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

from stack overflow answers because I was getting the error:

django.core.exceptions.ImproperlyConfigured:请求设置 USE_TZ,但未配置设置.您必须要么定义环境变量 DJANGO_SETTINGS_MODULE 或调用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.

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

but I don't have experience and I don't understand what was the error.

------------------- 更新----------------------

我已阅读 https://groups.google.com/forum/#!topic/django-users/bF_lRbzzguA 可能是那个

I have read on https://groups.google.com/forum/#!topic/django-users/bF_lRbzzguA that it could be that

问题是您的应用程序之一导入模型在其顶级 init.py 中.这是不支持的;为解释,你可以阅读https://docs.djangoproject.com/en/1.9/ref/applications/#how-applications-are-loaded

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"mypathdati_prova.xlsx"

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

现在我明白了:

运行时错误:模型类 myapp.models.mymodel 未声明显式 app_label 并且不在 INSTALLED_APPS 中的应用程序中.

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 .有什么问题?也许是在读另一套

But it is not true, since in settings.py I wrote my app name, and the project runs fine. Just the script does not work... It is like python cannot read my settings.py . What's the problem? Maybe is it reading another set

------------------- 更新----------------------

正如这里的建议 https://stackoverflow.com/a/38821174/7658051
我已将我的脚本 load_glossary.py 移动到

As suggested here https://stackoverflow.com/a/38821174/7658051
I have moved my script load_glossary.py into

myapp>管理>命令

myapp>management>commands


将我的 xlsx 文件复制到一个 csv
并更新代码如下:


made a copy of my xlsx file into a csv one
and updated the code as follows:

# 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 prompt 来解决它

And I am lunching it by typing into anaconda prompt

python ./manage.py load_glossary csv_file "mypathdati_prova.csv"

但后来我明白了

第 20 行,在句柄中dataReader = csv.reader(open(csv_file), delimiter=',', quotechar='"') FileNotFoundError: [Errno 2] 没有这样的文件或目录:'csv_file'

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

这次怎么了?

推荐答案

我通过替换这个解决了这个问题:

I solved the problem by substituting this:

import pandas as pd
from django.conf import settings

settings.configure()

from myapp.models import glossary_entry #this is line 7

path=r"mypathdati_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

这篇关于“django.core.exceptions.AppRegistryNotReady:应用程序尚未加载"尝试将数据加载到我的模型中时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆