简体   繁体   中英

Django Application: Model ImportError

I have a django 1.9 application using DRF with the following folder structure

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

In serializers.py I have an import:

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

In views.py I have these imports:

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:

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.

This would cause a circular import, since serializers.py imports models.py , and vise versa.

How to resolve this depends on what NotificationSerializer does. If it does not use the models, you might consider moving it to a utils file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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