Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

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

I have two apps named collection, accounts. Both apps have models defined. I am importing a model ReporterProfile from accounts app to collection. Similarly, a model Report from the app collection to accounts.

The Report model from collection app is called in a model class method in accounts app 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 app to collection app 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'm getting the error:

File "F:\project_name\accounts\models.py", line 8, in from collection.models import Report

File "F:\project_name\collection\models.py", line 2, in 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 wrong importing pattern. What should I do?

over 4 years ago · Santiago Trujillo
4 Respostas
Responde à pergunta

0

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()
over 4 years ago · Santiago Trujillo Relatório

0

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
over 4 years ago · Santiago Trujillo Relatório

0

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")
    ...
over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda