简体   繁体   English

Django应用程序:模型ImportError

[英]Django Application: Model ImportError

I have a django 1.9 application using DRF with the following folder structure 我有一个使用DRF的django 1.9应用程序,具有以下文件夹结构

- api/
- api/models.py
- api/serializers.py
- api/views.py
- manage.py

In serializers.py I have an import: 在serializers.py中我有一个导入:

from api.models import Model1, Model2, ...

In views.py I have these imports: 在views.py中我有这些导入:

from api.serializers import NotificationSerializer
from api.models import Model1, Model2, ...

Everything works fine until now. 到目前为止一切正常。 But after adding 但是在添加之后

from api.serializers import NotificationSerializer

in models.py, django starts complaining when I start the dev server: 在models.py中,当我启动dev服务器时,django开始抱怨:

File ".../api/serializers.py", line 3, in <module>
from api.models import Model1, Model2,...
ImportError: cannot import name Model1

I know that there must be a problem with the python import paths, but I can't figure it out. 我知道python导入路径一定存在问题,但我无法弄明白。

This would cause a circular import, since serializers.py imports models.py , and vise versa. 这将导致循环导入,因为serializers.py导入models.py ,反之亦然。

How to resolve this depends on what NotificationSerializer does. 如何解决这个问题取决于NotificationSerializer功能。 If it does not use the models, you might consider moving it to a utils file. 如果它不使用模型,您可以考虑将其移动到utils文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM