Search code examples
djangopython-3.xdjango-modelspython-packaging

How to fix 'ImportError' while working with multiple Django apps


I'm working with multiple apps in django and facing ImportError while running makemigrations command.
The import statements are as follows, appwise:

accounting/models.py
from activity.models import HistoryModel

activity/models.py
from user_management.models import Customer, Merchant, PassIssued
from accounting.models import ITMSCustomer

user_management/models.py
from accounting.models import Account, Transaction, Posting

I'm sure that the order of apps listed in INSTALLED_APPS matter and the order is:

'user_management',
'accounting',
'activity',

I get following error when I run makemigrations command:

  File "/home/abhishek/citycash/city-server/src/cityserver/user_management/models.py", line 4, in <module>
    from accounting.models import Account, Transaction, Posting
  File "/home/abhishek/citycash/city-server/src/cityserver/accounting/models.py", line 17, in <module>
    from activity.models import HistoryModel
  File "/home/abhishek/citycash/city-server/src/cityserver/activity/models.py", line 4, in <module>
    from user_management.models import Customer, Merchant, PassIssued
ImportError: cannot import name 'Customer'

I tried changing the order of apps in INSTALLED_APPS but I just ended up getting ImportError for different modules. I know this has something to do with the fact that all three apps are importing something from each other. How do I resolve this error?
Any help is appreciated. Thanks in advance.


Solution

  • To help someone in future facing the same problem, I finally ended up creating a new app (to have HistoryModel, BaseHistoryModel, etc.) and import it. Any other suggestions are welcome.