Django ImportError: cannot import name 'ReporterProfile' from partially initialized module 'accounts.models' (most likely due to a circular import)
Question:
I have two apps: collection
and accounts
, with both having models defined. I’m importing a model ReporterProfile
from accounts
to collection
. Similarly, I’m importing a model Report
from collection
to accounts
.
The Report
model from collection
is called in a model class method in accounts
like this:
from collection.models import Report
class ReporterProfile(models.Model):
....
def published_articles_number(self):
num = Report.objects.filter(reporterprofile=self.id).count()
return num
Similarly, I am importing ReporterProfile
and User
models from accounts
to collection
model like this:
from accounts.models import ReporterProfile, User
from <project_name> import settings
class Report(models.Model):
reporterprofile = models.ForeignKey(ReporterProfile, on_delete=models.CASCADE, verbose_name="Report Author")
...
class Comment(models.Model):
report = models.ForeignKey(Report, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="Comment by")
...
When running the server or makemigrations, I get the error:
File "F:project_nameaccountsmodels.py", line 8, in <module>
from collection.models import Report
File "F:project_namecollectionmodels.py", line 2, in <module>
from accounts.models import ReporterProfile, User
ImportError: cannot import name 'ReporterProfile' from partially initialized module 'accounts.models' (most likely due to a circular import) (F:project_nameaccountsmodels.py)
I think the error is coming because of a wrong importing pattern. What should I do?
Answers:
For ForeignKey
:
Instead of using reporterprofile = models.ForeignKey(ReporterProfile, ...)
, you can use reporterprofile = models.ForeignKey("accounts.ReporterProfile", ...)
, so you don’t have to import the model.
For preventing circulor import error :
Instead of using :
from accounts.models import ReporterProfile
[...]
foo = ReporterProfile()
You can use:
import accounts.models
[...]
foo = accounts.models.ReporterProfile()
Make sure you are writting the create models nane because I got the same issue and when take a look on my import I wrote userFormdata instead of userformData (I capitalized the ‘f’)
#wrong inport
from users_handler.models import userFormData
#Correct import
from users_handler.models import userformData
import the models like below
import collection
class ReporterProfile(models.Model):
....
def published_articles_number(self):
num = collection.models.Report.objects.filter(reporterprofile=self.id).count()
return num
similarly
import accounts
from <project_name> import settings
class Report(models.Model):
reporterprofile = models.ForeignKey(accounts.models.ReporterProfile, on_delete=models.CASCADE, verbose_name="Report Author")
...
class Comment(models.Model):
report = models.ForeignKey(accounts.models.Report, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="Comment by")
...
Make sure you are naming the directories and files name properly (without conflicting them with the name of other modules or files which django import as part of start up process). I name of directory app
and a file random.py
. Django while start looks for something similar and instead of fetching the actual module if ends up on this files which I created. And hence the error or circular import exceptions.
This happens because django process code in steps (order). for a circular import
one import is referring to another that has not been instantiated or installed
so it raises the circular import error
Simple fix is to bring down the import code below the class that requires it based on the order of definition of the apps in your django INSTALLED_APPS
this
Simple Answer:
**from accounts.models import ReporterProfile, User**
from <project_name> import settings
class Report(models.Model):
reporterprofile = models.ForeignKey(ReporterProfile, on_delete=models.CASCADE, verbose_name="Report Author")
...
class Comment(models.Model):
report = models.ForeignKey(Report, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="Comment by")
...
from collection.models import Report
should come after you have defined your ReportFile class in django
class ReportFile():
#fields
**.... make the import either within the class at the start of the proptery function
... or just directly below the class**
the pattern `from app_name.models import ClassModel` still works
the first line of code needed to be processed in the accounts.models file before it can be made available to the collections.models file
so that caused the error
- Answer => Wrong import Pattern
Then again , Refrain from using python inbuilt classes or functions names like collections from creating apps in django as this can cause serious technical issues
You should check the import function. Somewhere they collapsed with each other. I had the same problem. After that I checked with the import function and then removed the import function. After that it worked fine. I just share with you what I am facing and resolve by using this way.
I have two apps: collection
and accounts
, with both having models defined. I’m importing a model ReporterProfile
from accounts
to collection
. Similarly, I’m importing a model Report
from collection
to accounts
.
The Report
model from collection
is called in a model class method in accounts
like this:
from collection.models import Report
class ReporterProfile(models.Model):
....
def published_articles_number(self):
num = Report.objects.filter(reporterprofile=self.id).count()
return num
Similarly, I am importing ReporterProfile
and User
models from accounts
to collection
model like this:
from accounts.models import ReporterProfile, User
from <project_name> import settings
class Report(models.Model):
reporterprofile = models.ForeignKey(ReporterProfile, on_delete=models.CASCADE, verbose_name="Report Author")
...
class Comment(models.Model):
report = models.ForeignKey(Report, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="Comment by")
...
When running the server or makemigrations, I get the error:
File "F:project_nameaccountsmodels.py", line 8, in <module>
from collection.models import Report
File "F:project_namecollectionmodels.py", line 2, in <module>
from accounts.models import ReporterProfile, User
ImportError: cannot import name 'ReporterProfile' from partially initialized module 'accounts.models' (most likely due to a circular import) (F:project_nameaccountsmodels.py)
I think the error is coming because of a wrong importing pattern. What should I do?
For ForeignKey
:
Instead of using reporterprofile = models.ForeignKey(ReporterProfile, ...)
, you can use reporterprofile = models.ForeignKey("accounts.ReporterProfile", ...)
, so you don’t have to import the model.
For preventing circulor import error :
Instead of using :
from accounts.models import ReporterProfile
[...]
foo = ReporterProfile()
You can use:
import accounts.models
[...]
foo = accounts.models.ReporterProfile()
Make sure you are writting the create models nane because I got the same issue and when take a look on my import I wrote userFormdata instead of userformData (I capitalized the ‘f’)
#wrong inport
from users_handler.models import userFormData
#Correct import
from users_handler.models import userformData
import the models like below
import collection
class ReporterProfile(models.Model):
....
def published_articles_number(self):
num = collection.models.Report.objects.filter(reporterprofile=self.id).count()
return num
similarly
import accounts
from <project_name> import settings
class Report(models.Model):
reporterprofile = models.ForeignKey(accounts.models.ReporterProfile, on_delete=models.CASCADE, verbose_name="Report Author")
...
class Comment(models.Model):
report = models.ForeignKey(accounts.models.Report, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="Comment by")
...
Make sure you are naming the directories and files name properly (without conflicting them with the name of other modules or files which django import as part of start up process). I name of directory app
and a file random.py
. Django while start looks for something similar and instead of fetching the actual module if ends up on this files which I created. And hence the error or circular import exceptions.
This happens because django process code in steps (order). for a circular import
one import is referring to another that has not been instantiated or installed
so it raises the circular import error
Simple fix is to bring down the import code below the class that requires it based on the order of definition of the apps in your django INSTALLED_APPS
this
Simple Answer:
**from accounts.models import ReporterProfile, User**
from <project_name> import settings
class Report(models.Model):
reporterprofile = models.ForeignKey(ReporterProfile, on_delete=models.CASCADE, verbose_name="Report Author")
...
class Comment(models.Model):
report = models.ForeignKey(Report, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="Comment by")
...
from collection.models import Report
should come after you have defined your ReportFile class in django
class ReportFile():
#fields
**.... make the import either within the class at the start of the proptery function
... or just directly below the class**
the pattern `from app_name.models import ClassModel` still works
the first line of code needed to be processed in the accounts.models file before it can be made available to the collections.models file
so that caused the error
- Answer => Wrong import Pattern
Then again , Refrain from using python inbuilt classes or functions names like collections from creating apps in django as this can cause serious technical issues
You should check the import function. Somewhere they collapsed with each other. I had the same problem. After that I checked with the import function and then removed the import function. After that it worked fine. I just share with you what I am facing and resolve by using this way.