Akhi
Akhi

Reputation: 251

While upgrading Django 1.5 to 1.7 throws “Models aren't loaded yet" error

I am upgrading my django application from Django 1.5 to Django 1.7. While upgrading I am getting django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet error. I tried with some solution I got by searching. But nothing is worked for me. I think it because of one of my model. Please help me to fix this.

    Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/apps/config.py", line 197, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/venkat/sample-applications/wfmis-django-upgrade/wfmis-upgrade/django-pursuite/apps/admin/models/__init__.py", line 14, in <module>
    from occupational_standard import *
  File "/home/venkat/sample-applications/wfmis-django-upgrade/wfmis-upgrade/django-pursuite/apps/admin/models/occupational_standard.py", line 160, in <module>
    admin.site.register(OccupationalStandard, OccupationalStandardAdmin)
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 99, in register
    admin_class.check(model)
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 153, in check
    return cls.checks_class().check(cls, model, **kwargs)
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/contrib/admin/checks.py", line 497, in check
    errors.extend(self._check_list_filter(cls, model))
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/contrib/admin/checks.py", line 668, in _check_list_filter
    for index, item in enumerate(cls.list_filter)
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/contrib/admin/checks.py", line 713, in _check_list_filter_item
    get_fields_from_path(model, field)
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/contrib/admin/utils.py", line 457, in get_fields_from_path
    fields.append(parent._meta.get_field_by_name(piece)[0])
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/db/models/options.py", line 416, in get_field_by_name
    cache = self.init_name_map()
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/db/models/options.py", line 445, in init_name_map
    for f, model in self.get_all_related_m2m_objects_with_model():
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/db/models/options.py", line 563, in get_all_related_m2m_objects_with_model
    cache = self._fill_related_many_to_many_cache()
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/db/models/options.py", line 577, in _fill_related_many_to_many_cache
    for klass in self.apps.get_models():
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 168, in get_models
    self.check_models_ready()
  File "/home/venkat/sample-applications/wfmis-django-upgrade/venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")

This is my project strucutre.

Project Structure

setting.py

INSTALLED_APPS = (
'admin.apps.AdminConfig',
'account.apps.AccountConfig',
'...............'
)

wsgi.py

import os
import sys

PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, '../apps'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pursuite.settings.production")
import settings
import django.core.management
django.core.management.setup_environ(settings)  # Setup settings for core mgmt

utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')

command.validate()
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

apps/admin/apps.py

from django.apps import AppConfig

class AdminConfig(AppConfig):
    name = 'apps.admin'
    label = 'wfmis_admin'

apps/admin/models/occupational_standard.py

from tinymce.models import HTMLField
from django.db import models
from django.contrib import admin
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError
from haystack import indexes

from .validators import validate_os_code, validate_version
import admin.common as common

__all__ = ['OccupationalStandard', 'OccupationalStandardIndex']


class OccupationalStandard(models.Model):
    '''
        Occupational Standard
    '''
    class Meta:
        '''
            Meta properties for this model
        '''
        app_label = 'wfmis_admin'
        unique_together = ('code', 'version')

    code = models.CharField(
        max_length=9, default=None, validators=[validate_os_code],
        db_index=True,
    )
    version = models.CharField(
        max_length=8, default=None, validators=[validate_version],
        db_index=True,
    )
    is_draft = models.BooleanField(default=True, verbose_name="Draft")
    sub_sector = models.ForeignKey(
        'SubSector', db_index=True, verbose_name="Industry Sub-sector",
    )

    title = models.CharField(
        max_length=50, default=None, db_index=True, verbose_name="Unit Title",
    )
    description = models.TextField(default=None)
    scope = HTMLField(default=None)
    performace_criteria = HTMLField(default=None)
    knowledge = HTMLField(default=None)
    skills = HTMLField(default=None)
    attachment = models.FileField(upload_to='os_attachments')

    drafted_on = models.DateTimeField(auto_now_add=True)
    last_reviewed_on = models.DateTimeField(auto_now=True)  # Write date
    next_review_on = models.DateField()

    def __unicode__(self):
        '''
            Returns object display name. This comprises code and version.
            For example: SSC/O2601-V0.1
        '''
        return "%s-V%s%s (%s)" % (
            self.code, self.version, "draft" if self.is_draft else "",
            self.title,
        )

    @property
    def sector(self):
        """
        Returns sector corresponding to occupational standard.
        """
        return self.sub_sector.sector

    def get_absolute_url(self):
        '''
            get absolute url
        '''
        return reverse('occupational_standard', args=(self.code,))

    def clean(self):
        '''
            Validate model instance
        '''
        if OccupationalStandard.objects.filter(code=self.code, is_draft=True) \
                .exclude(pk=self.pk):
            # Check one OS should have one version in draft
            raise ValidationError(
                'There is already a version in draft for %s' % self.code
            )

Reference links : Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet

Upvotes: 0

Views: 116

Answers (2)

Sudoer-_-Nemo
Sudoer-_-Nemo

Reputation: 1

Stop the venv before upgrading django. Stop the server before upgrading. Update to 1.7 style wsgi handler.

Also, use pip to manage & upgrade packages, your script is bound to break the packages otherwise.

Upvotes: 0

VStoykov
VStoykov

Reputation: 1740

From the traceback I see the following:

  File "/home/venkat/sample-applications/wfmis-django-upgrade/wfmis-upgrade/django-pursuite/apps/admin/models/__init__.py", line 14, in <module>
    from occupational_standard import *
  File "/home/venkat/sample-applications/wfmis-django-upgrade/wfmis-upgrade/django-pursuite/apps/admin/models/occupational_standard.py", line 160, in <module>
    admin.site.register(OccupationalStandard, OccupationalStandardAdmin)

There is a call to admin.site.register in the models. Registering models should happen in admin not in models.

Upvotes: 1

Related Questions