一尘不染

Django 1.7升级错误:AppRegistryNotReady:模型尚未加载

django

我正在尝试将项目从Django 1.6升级到1.7。到目前为止,我已经使用相同的安装创建了一个新的virtualenv,并将Django版本升级到了新版本。我需要从南方升级,但是这样做有错误,所以我认为我最初只是尝试运行服务器,然后出现以下错误:

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.

有什么想法可能导致错误,以及如何解决该错误?


阅读 591

收藏
2020-04-01

共1个答案

一尘不染

问题在于此行(“ /Users/Name/Dev/tps/products/models.py”,第127行):

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

你尝试在导入时引用模型。在Django 1.7中不再可用。Django 1.7允许你仅在所有应用程序加载后才能使用模型。你应该将此调用移至的ready回调AppConfig,如下所示:

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

那么你应该参考这个AppConfig在__init__.py你的products应用程序:

default_app_config = 'products.apps.ProductsConfig'

哪里apps是你把配置模块的名称。

2020-04-01