ImportError: cannot import name update_all_contenttypes

ImportError: cannot import name update_all_contenttypes

我最近升级到 Django 1.8。在以前版本的 Django 中,可以使用以下导入:

from django.contrib.contenttypes.management import update_all_contenttypes

但是 update_all_contenttypes 似乎已在 Django 1.8 中悄悄删除(它在 1.7.7 中存在)。我在 1.8 release notes 中没有看到任何关于它被删除的信息...有谁知道该功能的现代替代品是什么?

目前还不清楚为什么在 1.8 中删除了该功能,但现代替代品似乎只是重新发明了那个轮子:

from django.apps import apps
from django.contrib.contenttypes.management import update_contenttypes

def update_all_contenttypes(**kwargs):
    for app_config in apps.get_app_configs():
        update_contenttypes(app_config, **kwargs)

似乎 django 团队删除了 update_contenttypes 功能,但在发行说明中没有提及,因为没有记录,public API。 (如这里所说:https://code.djangoproject.com/ticket/28092

现在您可以使用新函数 create_contenttypes,如您在此处所见: https://github.com/django/django/commit/6a2af01452966d10155b720f4f5e7b09c7e3e419

  • 转到系统中 'django.contrib.contenttypes' 的位置(检查 python 的 'site-packages' 文件夹)
  • 打开app.py(您可以在终端window中使用vi app.py)
  • 将'update_contenttypes'替换为'create_contenttypes'
  • 保存文件('ESC'、‘:’和‘wq’ - 在 vi​​ 编辑器中)

然后尝试运行服务器

创建一个虚拟环境,错误就会消失。

find here how to create a virtual_env: