ImportError:无法导入名称泛型

时间:2016-02-17 10:32:30

标签: python django generics django-1.9

我正在使用django 1.9中的eav-django(entity-attribute-value)。每当我执行命令./manage.py runserver时,我都会收到错误:

Unhandled exception in thread started by <function wrapper at 0x10385b500>
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/apps/config.py", line 202, 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/shakil_grofers/src/django-eav/eav/models.py", line 42, in <module>
    from django.contrib.contenttypes import generic

我试图通过添加:

来导入通用
from django.contrib.contenttypes import generic 

在models.py中。经过一些研究,我发现在Django 1.7中已经弃用了泛型,而在Django 1.9中已经不再使用了泛型。任何人都可以告诉我在Django 1.9中添加了哪些其他库以及如何使用它?

3 个答案:

答案 0 :(得分:16)

django.contrib.contenttypes模块已reorganized in Django 1.7

现在可以使用django.contrib.contenttypes.generic.GenericForeignKey代替django.contrib.contenttypes.fields.GenericForeignKey。请参阅文档中的示例代码:

https://docs.djangoproject.com/en/1.7/ref/contrib/contenttypes/#generic-relations

答案 1 :(得分:1)

此功能已移至.models和.fields模块中。 它只是删除的特定文件;通用关系功能仍然存在,只是拆分成单独的文件。

答案 2 :(得分:1)

从Python的官方网站更新Django.tagging包,而不是从Pip源更新,因为Pip没有最新版本。