Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ImportError: cannot import name 'ReporterProfile' from partially initialized module 'accounts.models' (most likely due to a circular import)

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_name\accounts\models.py", line 8, in <module>
    from collection.models import Report

File "F:\project_name\collection\models.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_name\accounts\models.py)

I think the error is coming because of a wrong importing pattern. What should I do?

like image 797
forest Avatar asked May 30 '20 09:05

forest


People also ask

How to resolve importerror cannot import name cannot import name?

ImportError: cannot import name ‘x1’ from partially initialized module ‘x’. To resolve the ImportError: Cannot import name, modify the x.py file. Instead of importing the y module at the start of the x.py file, write at the end of the file. Now rerun, and you can see the following output.

Why do I get a circular import error in Django?

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

How to fix import from module import * error?

Now that you got your answer what you did wrong, here is some actual help: Use from module import * (in some cases). This error might happen in case the name of your file is the same as the name of the package you connect. Just rename your file, and it will work. Show activity on this post.

Why is my Django import taking so long to start?

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 .


1 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()
like image 136
Blusky Avatar answered Oct 06 '22 19:10

Blusky