0

I have two apps... let's call them tiger and bird. At first, I was using models from tiger in the bird app. This is how I did the importing in bird/models.py:

from django.db import models
from tiger.models import TigerClass, AnotherTigerClass

This worked well. There is no problem with me importing the TigerClass and OtherTigerClass into the bird app.

Now, I want to also do the opposite. So in the tiger app I want to import a model from the bird app. As far as I can find in the manual, there are no restrictions on this and I should be able to use the same syntax. So this is what I do in tiger/models.py:

from django.db import models
from bird.models import BirdClass

However, the moment I do this, my app throws a very odd error:

web_1      | Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fac830d1730>
web_1      | Traceback (most recent call last):
web_1      |   File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
web_1      |     fn(*args, **kwargs)
web_1      |   File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
web_1      |     autoreload.raise_last_exception()
web_1      |   File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
web_1      |     raise _exception[1]
web_1      |   File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 327, in execute
web_1      |     autoreload.check_errors(django.setup)()
web_1      |   File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
web_1      |     fn(*args, **kwargs)
web_1      |   File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
web_1      |     apps.populate(settings.INSTALLED_APPS)
web_1      |   File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
web_1      |     app_config.import_models()
web_1      |   File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
web_1      |     self.models_module = import_module(models_module_name)
web_1      |   File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
web_1      |     return _bootstrap._gcd_import(name[level:], package, level)
web_1      |   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
web_1      |   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
web_1      |   File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
web_1      |   File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
web_1      |   File "<frozen importlib._bootstrap_external>", line 678, in exec_module
web_1      |   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1      |   File "/code/tiger/models.py", line 3, in <module>
web_1      |     from bird.models import ProcessType
web_1      |   File "/code/bird/models.py", line 2, in <module>
web_1      |     from tiger.models import TigerClass, AnotherTigerClass
web_1      | ImportError: cannot import name 'TigerClass'

So now suddenly it is complaining that it can not import TigerClass into the bird app. The the weird thing is that this worked just fine before! I added a new import statement in the tiger/models.py file, but now there is an error in the bird/models.py file. I'm at a loss why this is a problem. I've triple checked names, I've copied the same statement from other apps, I've tried importing different classes within the same app... all without any changes.

  • Check this answer about [Circular imports in Django](https://stackoverflow.com/questions/4379042/django-circular-model-import-issue) – Bijoy Jun 27 '18 at 05:31
  • Thanks. I didn't know the term circular imports so that's why I couldn't find anything when searching. –  Jun 27 '18 at 05:31

1 Answers1

-1

You are in the cycle, you are trying to import tiger model in bird and bird model in tiger. One solution is to use the string representation of the model for the relationship, in that case, you don't need to import. Or import in model file not do the cyclic import.

aman kumar
  • 3,086
  • 1
  • 17
  • 24