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

214
Visualizações
ImportError: cannot import name 'Affiliate' from partially initialized module 'affiliate.models' (most likely due to a circular import)

I'm getting a circular import error in django and can't seem to solve it. here's my models.py in affiliate(app)

from member.models import Member

class SubAffiliate(models.Model):
    member_id = models.ForeignKey(Member, on_delete=models.CASCADE)

and here's my models.py in member(app)

from affiliate.models import Affiliate

class Member(models.Model):
    affiliates = models.ManyToManyField(Affiliate, blank=True, related_name="members_affiliate")

for solving the problem I tried importing like this

import affiliate.models

and there use it like this

affiliate.models.Affiliate

then I get this error AttributeError: module 'affiliate' has no attribute 'models'

what should I do to resolve this error. Thank You!

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

0

The two models can not import each other. You can work with a string literal in case you need to refer to another module's model:

# no import from member.models!

class SubAffiliate(models.Model):
    member = models.ForeignKey(
        'member.Member',
        on_delete=models.CASCADE
    )

and for the other models.py file, you can also work with a string with the app_name.ModelName:

# no import from affiliate.models

class Member(models.Model):
    affiliates = models.ManyToManyField(
        'affiliate.Affiliate',
        blank=True,
        related_name='members_affiliate'
    )
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