0
在我的Django项目,我有以下:Django的导入错误看起来像一个循环
APPS 1/models.py:帖子(模型)
APPS2/models.py:博客(模型)
APPS 2 /功能。 py:get_blogs(方法)
apps1/models.py文件从apps2/models.py中导入博客模型。
apps2/models.py文件从apps2/functions.py中导入get_blogs方法。
apps2/functions.py文件从apps1/models.py中导入Post模型。
我收到以下错误:
ImportError at/
cannot import name Post
Traceback
admin.autodiscover()
<in file apps1/models.py>
from apps2.models import Blogs
<in file apps2/models.py>
from apps2.functions import get_blogs
<in file apps2/functions.py>
from apps1.models import Post
我想这可能是该admin.autodiscover是先进口后的模型通过导入环路,它试图再次导入。虽然我试图改变它:
from apps1.models import Post as OtherPost
但这并没有帮助。任何想法为什么发生这种情况?是因为现在有一个循环吗?
只是一个想法:你们的模型(实际班)两个目录深? (从项目根开始计算)在这种情况下,您是否在每个模型的Meta类中定义了app_label? –
否。模型位于应用程序目录内的models.py文件中。没有人定义了app_label。 – mhost
我刚刚尝试注释掉admin.autodiscover()并且没有任何更改(Traceback稍有变化,但错误是相同的并发生在同一个地方(当在apps2/functions.py文件中导入Post时)) – mhost