将 Django 1.5 升级到 1.7 时抛出“模型尚未加载”错误
While upgrading Django 1.5 to 1.7 throws “Models aren't loaded yet" error
我正在将我的 Django 应用程序从 Django 1.5
升级到 Django 1.7
。升级时出现 django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
错误。我尝试了一些通过搜索得到的解决方案。但是对我来说没有任何用处。我认为这是因为我的一个模型。请帮我解决这个问题。
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.")
这是我的项目结构。
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
)
参考链接:Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
从回溯中我看到以下内容:
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)
models
中有对admin.site.register
的调用。注册模型应该发生在 admin
而不是 models
.
在升级 django 之前停止 venv。
升级前停止服务器。
更新到 1.7 风格的 wsgi 处理程序。
此外,使用 pip 来管理和升级包,否则您的脚本必然会破坏包。
我正在将我的 Django 应用程序从 Django 1.5
升级到 Django 1.7
。升级时出现 django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
错误。我尝试了一些通过搜索得到的解决方案。但是对我来说没有任何用处。我认为这是因为我的一个模型。请帮我解决这个问题。
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.")
这是我的项目结构。
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
)
参考链接:Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
从回溯中我看到以下内容:
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)
models
中有对admin.site.register
的调用。注册模型应该发生在 admin
而不是 models
.
在升级 django 之前停止 venv。 升级前停止服务器。 更新到 1.7 风格的 wsgi 处理程序。
此外,使用 pip 来管理和升级包,否则您的脚本必然会破坏包。