无法将 Django 从 1.7 迁移到 1.8

Posted

技术标签:

【中文标题】无法将 Django 从 1.7 迁移到 1.8【英文标题】:Cannot migrate Django from 1.7 to 1.8 【发布时间】:2020-09-29 23:39:39 【问题描述】:

将 django 1.7 升级到 1.8 时(最终目标是将 1.4 迁移到 1.11LTS) 我被要求从我的 INSTALLED_APPS 中删除 SOUTH

很好,但是我明白了:

raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. 

所以我尝试在设置文件中添加:

import django
django.setup()

但它在运行时带来了这个错误,这让我觉得这不是正确的方法:

AUTH_USER_MODEL refers to model 'auth.User' that has not been installed

我的 INSTALLED_APPS:

'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
'mptt',
# 'south',
'main',
'proxy',
'picocms',
'django.contrib.admin',
'reports',
'django_orphaned'

请注意,如果我注释 'main'(我的主代码文件夹)和 'picocms'(一个过时的库),AppRegistryNotReady 异常不会再引发(但显然项目没有它就无法工作)

有人成功地解决了同样的问题吗?

stackTrace(供参考)

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python@2/2.7.15_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File ".../workspace/django/cms/main/models.py", line 19, in <module>
    from picocms.models import CMSModel, CMSCategory, ActiveModelManager, PublicModelManager
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/picocms/models.py", line 13, in <module>
    class CMSCategory(MPTTModel):
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/mptt/models.py", line 189, in __new__
    return meta.register(cls)
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/mptt/models.py", line 273, in register
    manager.init_from_model(cls)
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/mptt/managers.py", line 58, in init_from_model
    [tree_field] = [fld for fld in model._meta.get_fields_with_model() if fld[0].name == self.tree_id_attr]
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/db/models/options.py", line 56, in wrapper
    return fn(*args, **kwargs)
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/db/models/options.py", line 432, in get_fields_with_model
    return [self._map_model(f) for f in self.get_fields()]
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/db/models/options.py", line 740, in get_fields
    return self._get_fields(include_parents=include_parents, include_hidden=include_hidden)
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/db/models/options.py", line 802, in _get_fields
    all_fields = self._relation_tree
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/utils/functional.py", line 60, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/db/models/options.py", line 709, in _relation_tree
    return self._populate_directed_relation_graph()
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/db/models/options.py", line 681, in _populate_directed_relation_graph
    all_models = self.apps.get_models(include_auto_created=True)
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File ".../.virtualenvs/migre/lib/python2.7/site-packages/django/apps/registry.py", line 168, in get_models
    self.check_models_ready()
  File ".../.virtualenvs/migre/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.

【问题讨论】:

不要将django.setup() 添加到settings.py,这是一个不正确的建议。 mainpicocms 中可能有一些代码在加载应用程序时尝试过早导入模型。 AppRegistryNotReady 错误的完整回溯可能会显示问题发生的位置。 如果这与工作有关,我会花一点时间让某人知道不再支持 1.11。您的时间可能最好花在用 python 3 和 Django 2.2 编写项目的新版本上。 djangoproject.com/download @Alasdair:它似乎来自 picocms 和 mptt,但我不知道如何修复它(一切都适用于 1.7)我添加了 stackTrace 以防万一。 您可能需要将 mptt 和/或 picocms 升级到支持 Django 1.8 的版本。 【参考方案1】:

我升级了 picocms 已弃用的项目并将 mptt 版本切换到 0.9.0 并且它工作正常。

但实际上,我最好花时间在 python 3 和 Django 2.2 中编写项目的新版本

【讨论】:

以上是关于无法将 Django 从 1.7 迁移到 1.8的主要内容,如果未能解决你的问题,请参考以下文章

Django 1.8 迁移无法将列 ID 转换为整数

恢复 Django 1.7 RemoveField 迁移

将数据库从本地开发迁移到 Heroku-Django 1.8

从 South 迁移到 Django 1.7 迁移:可交换依赖项

使用 --fake 后如何在 django 1.8 上重做迁移

从 1.5 迁移到 1.7 时的单元测试警告