1

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.

Michael
  • 538
  • 1
  • 7
  • 16

1 Answers1

0

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.

bear
  • 123
  • 1
  • 9