2016-05-29 174 views
2

我有兩個文件在同一文件夾(應用程序)feeddjango導入錯誤

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 

爲什麼會出現這種錯誤,以及如何我解決這個問題嗎?

+2

您正面臨循環導入問題。從'managers.py'文件的頂部刪除'.models import Feed',你就會很好。無論何時您需要該模型,都可以將其導入管理器函數本身。 –

回答

4

您已經介紹了一個循環導入:models模塊取決於managers模塊,而模塊又取決於models模塊。

這兩個模塊都無法解決,因爲它們的依賴關係無法解析。嘗試重新組織你的模塊。