I think this is more a python question than Django.
But basically I’m doing at Model A:
from myproject.modelb.models import ModelB
and at Model B:
from myproject.modela.models import ModelA
Result:
cannot import name ModelA
Am I doing something forbidden? Thanks
A Python module is imported by executing it top to bottom in a new namespace. When module A imports module B, the evaluation of A.py is paused until module B is loaded. When module B then imports module A, it gets the partly-initialized namespace of module A — in your case, it lacks the
ModelAclass because the import ofmyproject.modelb.modelshappens before the definition of that class.In Django you can fix this by referring to a model by name instead of by class object. So, instead of saying
you would use (without the import):