Django 1.7升级错误:感谢的准备:模型还没有加载。

时间:2022-03-18 20:26:04

I am trying to upgrade a project from Django 1.6 to 1.7. So far, I have created a new virtualenv with all the same installs and upgraded the Django version to the new release. I need to upgrade from South, but had errors doing so, so I thought I'll initially just try runserver, and I get the following error:

我正在尝试将一个项目从Django 1.6升级到1.7。到目前为止,我已经创建了一个新的virtualenv,并将Django版本升级到新版本。我需要从South进行升级,但是有错误,所以我想先试试runserver,得到如下错误:

Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
        utility.execute()
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
        django.setup()
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
        app_config.import_models(all_models)
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/apps/config.py", line 197, in import_models
        self.models_module = import_module(models_module_name)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
        __import__(name)
      File "/Users/Name/Dev/tps/products/models.py", line 127, in <module>
        watson.register(Product.objects.exclude(productimage=None))
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
        return getattr(self.get_queryset(), name)(*args, **kwargs)
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/db/models/query.py", line 698, in exclude
        return self._filter_or_exclude(True, *args, **kwargs)
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/db/models/query.py", line 707, in _filter_or_exclude
        clone.query.add_q(~Q(*args, **kwargs))
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1287, in add_q
        clause, require_inner = self._add_q(where_part, self.used_aliases)
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1314, in _add_q
        current_negated=current_negated, connector=connector)
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1138, in build_filter
        lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1076, in solve_lookup_type
        _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1339, in names_to_path
        field, model, direct, m2m = opts.get_field_by_name(name)
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/db/models/options.py", line 416, in get_field_by_name
        cache = self.init_name_map()
      File "/Users/Name/.virtualenvs/test17/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 "/Users/Name/.virtualenvs/test17/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 "/Users/Name/.virtualenvs/test17/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 "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
        result = user_function(*args, **kwds)
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/apps/registry.py", line 168, in get_models
        self.check_models_ready()
      File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready
        raise AppRegistryNotReady("Models aren't loaded yet.")
    django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

Any ideas what might be causing the error and how to fix it?

你知道是什么导致了这个错误吗?

4 个解决方案

#1


22  

The problem is with this line ("/Users/Name/Dev/tps/products/models.py", line 127):

问题在于这一行(“/用户/名称/Dev/tps/产品/模型”)。py”,第127行):

watson.register(Product.objects.exclude(productimage=None))

You try to reference a model at the import time. It is no longer possible in Django 1.7. Django 1.7 allows you to use your models only after all of the applications are loaded. You should move this call to the ready callback of AppConfig, like this:

您尝试在导入时引用一个模型。在Django 1.7中已经不可能了。Django 1.7允许在加载所有应用程序后才使用模型。您应该将此调用移动到AppConfig的ready回调,如下所示:

from django.apps import AppConfig


class ProductsConfig(AppConfig):
    name = 'products'

    def ready(self):
        Product = self.get_model('Product')
        watson.register(Product.objects.exclude(productimage=None))

Then you should reference this AppConfig in the __init__.py of your products app:

然后您应该在__init__中引用这个AppConfig。您的产品app py:

default_app_config = 'products.apps.ProductsConfig'

Where apps is the name of the module where you put the config.

应用程序是放置配置的模块的名称。

Relevant Django doc: https://docs.djangoproject.com/en/dev/ref/applications/

Django相关文档:https://docs.djangoproject.com/en/dev/ref/applications/

Overall, because of this change, migrating to Django 1.7 is not as easy as one would like it to be. Here are some troubleshooting tips: https://docs.djangoproject.com/en/1.7/ref/applications/#troubleshooting

总的来说,由于这种变化,迁移到Django 1.7并不像人们希望的那样容易。以下是一些故障排除技巧:https://docs.djangoproject.com/en/1.7/ref/applications/#故障排除

#2


15  

I was getting this error when I updated my django project template to 1.7. One thing that changed is the wsgi.py file so that needed some updating. Here is my traceback:

当我将django项目模板更新为1.7时,我得到了这个错误。改变的是wsgi。py文件需要一些更新。这是我的回溯:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 93, in handle
    self.handle_request(listener, req, client, addr)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 134, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/handlers/wsgi.py", line 168, in __call__
    self.load_middleware()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/handlers/base.py", line 46, in load_middleware
    mw_instance = mw_class()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/middleware/locale.py", line 23, in __init__
    for url_pattern in get_resolver(None).url_patterns:
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/urlresolvers.py", line 372, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/urlresolvers.py", line 366, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/kpmteam/staging/site/kpm/urls.py", line 7, in <module>
    admin.autodiscover()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/contrib/admin/__init__.py", line 23, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/utils/module_loading.py", line 67, in autodiscover_modules
    for app_config in apps.get_app_configs():
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
AppRegistryNotReady: Apps aren't loaded yet.

This is what the wsgi.py file looked like:

这就是wsgi。py文件看起来像:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

And now (running Django 1.7 with gunicorn 19.1.0):

现在(用gunicorn 19.1.0运行Django 1.7):

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Changing the last two lines fixed it. Note your DJANGO_SETTINGS_MODULE might be different, typically it is "project_name.settings"

换了最后两行就把它修好了。注意,DJANGO_SETTINGS_MODULE可能不同,通常是"project_name.settings"

#3


14  

I'm late to the party, but if you're using django-registration, you'll have to switch to django-regitration-redux.

我迟到了,但如果你用的是django-注册,你就得转到django-regitr -redux。

https://pypi.python.org/pypi/django-registration-redux/

https://pypi.python.org/pypi/django-registration-redux/

See this answer: Django-registration compatibility issue with django 1.7

参见这个答案:与django 1.7的django注册兼容性问题

#4


4  

I found the solution doing:

我发现解决方案是:

import django

django.setup()

django.setup()

#1


22  

The problem is with this line ("/Users/Name/Dev/tps/products/models.py", line 127):

问题在于这一行(“/用户/名称/Dev/tps/产品/模型”)。py”,第127行):

watson.register(Product.objects.exclude(productimage=None))

You try to reference a model at the import time. It is no longer possible in Django 1.7. Django 1.7 allows you to use your models only after all of the applications are loaded. You should move this call to the ready callback of AppConfig, like this:

您尝试在导入时引用一个模型。在Django 1.7中已经不可能了。Django 1.7允许在加载所有应用程序后才使用模型。您应该将此调用移动到AppConfig的ready回调,如下所示:

from django.apps import AppConfig


class ProductsConfig(AppConfig):
    name = 'products'

    def ready(self):
        Product = self.get_model('Product')
        watson.register(Product.objects.exclude(productimage=None))

Then you should reference this AppConfig in the __init__.py of your products app:

然后您应该在__init__中引用这个AppConfig。您的产品app py:

default_app_config = 'products.apps.ProductsConfig'

Where apps is the name of the module where you put the config.

应用程序是放置配置的模块的名称。

Relevant Django doc: https://docs.djangoproject.com/en/dev/ref/applications/

Django相关文档:https://docs.djangoproject.com/en/dev/ref/applications/

Overall, because of this change, migrating to Django 1.7 is not as easy as one would like it to be. Here are some troubleshooting tips: https://docs.djangoproject.com/en/1.7/ref/applications/#troubleshooting

总的来说,由于这种变化,迁移到Django 1.7并不像人们希望的那样容易。以下是一些故障排除技巧:https://docs.djangoproject.com/en/1.7/ref/applications/#故障排除

#2


15  

I was getting this error when I updated my django project template to 1.7. One thing that changed is the wsgi.py file so that needed some updating. Here is my traceback:

当我将django项目模板更新为1.7时,我得到了这个错误。改变的是wsgi。py文件需要一些更新。这是我的回溯:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 93, in handle
    self.handle_request(listener, req, client, addr)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 134, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/handlers/wsgi.py", line 168, in __call__
    self.load_middleware()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/handlers/base.py", line 46, in load_middleware
    mw_instance = mw_class()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/middleware/locale.py", line 23, in __init__
    for url_pattern in get_resolver(None).url_patterns:
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/urlresolvers.py", line 372, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/urlresolvers.py", line 366, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/kpmteam/staging/site/kpm/urls.py", line 7, in <module>
    admin.autodiscover()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/contrib/admin/__init__.py", line 23, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/utils/module_loading.py", line 67, in autodiscover_modules
    for app_config in apps.get_app_configs():
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
AppRegistryNotReady: Apps aren't loaded yet.

This is what the wsgi.py file looked like:

这就是wsgi。py文件看起来像:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

And now (running Django 1.7 with gunicorn 19.1.0):

现在(用gunicorn 19.1.0运行Django 1.7):

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Changing the last two lines fixed it. Note your DJANGO_SETTINGS_MODULE might be different, typically it is "project_name.settings"

换了最后两行就把它修好了。注意,DJANGO_SETTINGS_MODULE可能不同,通常是"project_name.settings"

#3


14  

I'm late to the party, but if you're using django-registration, you'll have to switch to django-regitration-redux.

我迟到了,但如果你用的是django-注册,你就得转到django-regitr -redux。

https://pypi.python.org/pypi/django-registration-redux/

https://pypi.python.org/pypi/django-registration-redux/

See this answer: Django-registration compatibility issue with django 1.7

参见这个答案:与django 1.7的django注册兼容性问题

#4


4  

I found the solution doing:

我发现解决方案是:

import django

django.setup()

django.setup()