在django中导入错误

时间:2016-05-29 08:33:47

标签: python django

我在同一个文件夹(app)feed中有两个文件。

models.py

from django.db import models
from .managers import FeedManager

class Feed(models.Model):
    #fields for my model

managers.py

from django.db import models

from .models import Feed

class FeedManager(models.Manager):
    def get_queryset(self, *args, **kwargs):
        return super(FeedManager, self).get_queryset(*args, **kwargs)

运行服务器时出现以下错误:

      Unhandled exception in thread started by <function wrapper at 0x04064370>
Traceback (most recent call last):
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\utils\autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\core\management\commands\runserver.py", line 107, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\utils\autoreload.py", line 252, in raise_last_exception
    six.reraise(*_exception)
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\utils\autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:\Users\Sumit\Desktop\django\venv\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Users\Sumit\Desktop\django\network\feed\models.py", line 7, in <module>
    from .managers import FeedManager
  File "C:\Users\Sumit\Desktop\django\network\feed\managers.py", line 3, in <module>
    from .models import Feed
ImportError: cannot import name Feed

为什么我会收到此错误,如何解决此问题?

1 个答案:

答案 0 :(得分:4)

您已经介绍了一个循环导入:models模块依赖于managers模块,而models模块依赖于var DesafioTag = new HashSet<int>( desafio.Tags.Select(a => a.Id)); 模块。

由于无法解决依赖关系,因此无法解析任何模块。尝试重新组织您的模块。