Skip to content
Advertisement

“django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet” when trying to load data into my model

I am developing an app in Django.

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

But when I run it from Anaconda prompt, I get

File “load_glossary.py”, line 7, in module …

raise AppRegistryNotReady(“Apps aren’t loaded yet.”) django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet.

What’s the problem?

Please note:

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

Please note:

I copy pasted

from django.conf import settings

settings.configure()

from stack overflow answers because I was getting the error:

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.

Update

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

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

Update

I changed the file as following:

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

And now I get:

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

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

Update

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

myapp>management>commands


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'
                
                )

And I am lunching it by typing into anaconda prompt

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

But then I get

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

What’s wrong this time?

Advertisement

Answer

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

with this:

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
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement