Django 1.9 迁移问题

Posted

技术标签:

【中文标题】Django 1.9 迁移问题【英文标题】:Django 1.9 Migration Problems 【发布时间】:2016-07-29 01:46:33 【问题描述】:

我正在尝试将我的 1.8 应用程序转换为 Django 1.9。简单地为旧数据库创建迁移就可以了,但是当我尝试使用 migrate 创建一个新数据库时,我得到:

"Error creating new content types. Please make sure contenttypes " RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.

我已经对此进行了相当多的测试,如果我可以重新构建它我在寻找。似乎在迁移发生之前调用了 Django/core/management/base.py 第 398 行 -> self.check() ,缺少数据库会引发错误并在设置数据库之前退出(catch 22)。我已经通过注释掉它来测试它,这也将允许它构建数据库。

对于解决此问题的好方法或我的应用程序中可能存在的潜在问题,有人有什么建议吗?

编辑 完整的堆栈跟踪:

(test) C:\website\test\rpi_csdt_community>python manage.py migrate
C:\website\test\rpi_csdt_community\collected_static/
C:\website\test\rpi_csdt_community\rpi_csdt_community\urls.py:27: RemovedInDjang
o110Warning: Support for string view arguments to url() is deprecated and will b
e removed in Django 1.10 (got rpi_csdt_community.views.home). Pass the callable
instead.
  url(r'^$', 'rpi_csdt_community.views.home', , 'home'),

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\website\test\lib\site-packages\django\core\management\__init__.py", l
ine 353, in execute_from_command_line
    utility.execute()
  File "C:\website\test\lib\site-packages\django\core\management\__init__.py", l
ine 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\website\test\lib\site-packages\django\core\management\base.py", line
348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\website\test\lib\site-packages\django\core\management\base.py", line
398, in execute
    self.check()
  File "C:\website\test\lib\site-packages\django\core\management\base.py", line
426, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\website\test\lib\site-packages\django\core\checks\registry.py", line
75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\website\test\lib\site-packages\django\core\checks\urls.py", line 13,
in check_url_config
    return check_resolver(resolver)
  File "C:\website\test\lib\site-packages\django\core\checks\urls.py", line 23,
in check_resolver
    for pattern in resolver.url_patterns:
  File "C:\website\test\lib\site-packages\django\utils\functional.py", line 33,
in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\website\test\lib\site-packages\django\core\urlresolvers.py", line 417
, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\website\test\lib\site-packages\django\utils\functional.py", line 33,
in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\website\test\lib\site-packages\django\core\urlresolvers.py", line 410
, in urlconf_module
    return import_module(self.urlconf_name)
  File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\website\test\rpi_csdt_community\rpi_csdt_community\urls.py", line 29,
 in <module>
    url(r'', include('project_share.urls')),
  File "C:\website\test\lib\site-packages\django\conf\urls\__init__.py", line 52
, in include
    urlconf_module = import_module(urlconf_module)
  File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\website\test\rpi_csdt_community\project_share\urls.py", line 4, in <m
odule>
    from project_share.views import ProjectList, ProjectTagList, ApplicationList
, DemoList
  File "C:\website\test\rpi_csdt_community\project_share\views.py", line 54, in
<module>
    class ProjectList(SearchableListMixin, SortableListMixin, ListView):
  File "C:\website\test\rpi_csdt_community\project_share\views.py", line 59, in
ProjectList
    queryset = Project.approved_projects().all()
  File "C:\website\test\rpi_csdt_community\project_share\models.py", line 150, i
n approved_projects
    return Project.objects.filter(approved=True)
  File "C:\website\test\lib\site-packages\django\db\models\manager.py", line 122
, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\website\test\lib\site-packages\secretballot\__init__.py", line 58, in
 get_queryset
    content_type = ContentType.objects.get_for_model(self.model).id
  File "C:\website\test\lib\site-packages\django\contrib\contenttypes\models.py"
, line 80, in get_for_model
    "Error creating new content types. Please make sure contenttypes "
RuntimeError: Error creating new content types. Please make sure contenttypes is
 migrated before trying to migrate apps individually.

【问题讨论】:

你能发布整个堆栈跟踪吗?它闻起来像是您的应用程序中的初始化问题。 Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually的可能重复 这不是那个问题的重复。尚未创建 contenttypes 表。原因和解决方法都不一样。 【参考方案1】:

通过替换修复:

queryset = Project.approved_projects().all()

在我看来:

def get_queryset(self):
    return Project.approved_projects().all()

我猜它甚至在构建数据库之前就尝试测试视图......愚蠢的错误。但我把它留在这里,以防其他人与之斗争。

【讨论】:

以上是关于Django 1.9 迁移问题的主要内容,如果未能解决你的问题,请参考以下文章

Django 1.9 在迁移中删除外键

Python Django 1.9 迁移错误“创建新内容类型时出错...”

如何在 django 1.9+ 中合并连续的数据库迁移?

尝试在 Django 1.9 中迁移——奇怪的 SQL 错误 "django.db.utils.OperationalError: near ")": syntax er

刷新单应用程序 django 1.9

从 Django 1.6 升级到 1.9:python manage.py migrate 失败