Cannot import models from another app in Django

If you want to import only some specific module then do not use import *.

It will take more time load your all library and so can affect the speed of your app also.

If you want to use few modules from your second app then just add module name instead of whole libraries something like this:

from app2.models import Module1, Module2

or it may be circular import issue as other clarify.

Thanks.


This might be due to circular import issues. To avoid this you should load the model dynamically:

For recent versions of django (1.7+) use the application registry:

from django.apps import apps
MyModel1 = apps.get_model('app1', 'MyModel1')

For earlier django versions (<1.7):

from django.db.models.loading import get_model
MyModel1 = get_model('app1', 'MyModel1')

Note 1: If you want to define a ForeignKey relationship, there is no need for a separate import statement. Django has you covered on this:

If app1 is an installed app, you should define the ForeignKey relationship as follows:

# in app2.py
class MyModel2(models.Model):
   mymodel1 = models.ForeignKey('app1.MyModel1')

Note 2: The get_model only works if app1 is an installed app and MyModel1 is the model you want to import from app1.

Note 3: Try to avoid wildcard import (from ... import *), as this is bad practice.


It's definitely a circular import.

But i think is what you need is to use models as some sort of RetationFields(ForeignKey, ManyToManyField or OneToOneField) arguments. So you need to skip import and use as so:

# app1/models.py
class Model1(models.Model):
    relation_field = models.ForeignKey('app2.Model2')

From docs:

If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself

To refer to models defined in another application, you can explicitly specify a model with the full application label

Just put str object as first argument to relation fields that leeds to <app_name>.<Model_name>.

Note: it's better to avoid importing everything from module(from <module_name> import *)