Most likely due to a circular import (How to Resolve)

Asked

Viewed 3,140 times

3

Good night. I’m new in Django/Python, I’m developing a system where I have a circular relationship, I have the class person and the company, where a person belongs to a company and a company is a person. According to the following classes:

class Pessoa(models.Model):
    TIPO_CADASTRO_CHOICES = (
        (1, ('1. FUNCIONÁRIO')),
        (2, ('2. CLIENTE')),
        (3, ('3. FORNECEDOR')),
        (4, ('4. TRANSPORTADOR')),
    )

    TIPO_PESSOA_CHOICES = (
        (1, ('1. JURÍDICA')),
        (2, ('2. FISÍCA')),
    )

    nome = models.CharField(
        max_length=100
    )

    empresa = models.ForeignKey(
        on_delete=models.PROTECT,
        Empresa,
        null=True
    )

    tipo_cadastro = models.IntegerField(
        ('Tipo Cadastro'),
        choices=TIPO_CADASTRO_CHOICES,
        default=3
    )

    tipo_pessoa = models.IntegerField(
        ('Tipo Pessoa'),
        choices=TIPO_PESSOA_CHOICES,
        default=1
    )





class Empresa(models.Model):
    pessoa = models.ForeignKey(
        Pessoa,
        on_delete=models.PROTECT,
        verbose_name='pessoa'
    )

    cnae= models.IntegerField(
        blank=True
    )

    corporacao = models.ForeignKey(
        Corporacao,
        on_delete=models.PROTECT
    )




class Corporacao(models.Model):
    nome = models.CharField(
        max_length=100
    )

In that scenario when executing makemigrations, I have the following error:

File "D: Desenv Projects Python neosis apps core models pessoa.py", line 3, in from apps.core.models.empresas import Empresa File "D: Develop Python Projects neosis apps core models companies.py", line 5, in from . person import Person Import: cannot import name 'Pessoa' from partially initialized module 'apps.core.models.pessoa' (Most likely due to a circular import)

How could I fix this?

  • I’m wanting to do the same, but I was left with a doubt in your Company Class, in your case a company can belong (relates) to several people ? Sorry if I’m wrong, I’m at the beginning of learning with Django.

1 answer

1

Pessoa is calling for Empresa which is being declared just below, so how the code runs sequentially when it comes to Pessoa to Empresa it still won’t exist, to solve this is very simple just put in string the model name, see the official documentation from Django for more examples, I particularly prefer to be a little more explicit by talking about which app and then the model instead of just talking about the model.

class Pessoa(models.Model):
    TIPO_CADASTRO_CHOICES = (
        (1, ('1. FUNCIONÁRIO')),
        (2, ('2. CLIENTE')),
        (3, ('3. FORNECEDOR')),
        (4, ('4. TRANSPORTADOR')),
    )

    TIPO_PESSOA_CHOICES = (
        (1, ('1. JURÍDICA')),
        (2, ('2. FISÍCA')),
    )

    nome = models.CharField(
        max_length=100
    )

    empresa = models.ForeignKey(
        on_delete=models.PROTECT,
        "Empresa",
        null=True
    )

    tipo_cadastro = models.IntegerField(
        ('Tipo Cadastro'),
        choices=TIPO_CADASTRO_CHOICES,
        default=3
    )

    tipo_pessoa = models.IntegerField(
        ('Tipo Pessoa'),
        choices=TIPO_PESSOA_CHOICES,
        default=1
    )





class Empresa(models.Model):
    pessoa = models.ForeignKey(
        "Pessoa", # aqui não é mais necessário mas só pra manter o padrão
        on_delete=models.PROTECT,
        verbose_name='pessoa'
    )

    cnae= models.IntegerField(
        blank=True
    )

    corporacao = models.ForeignKey(
        Corporacao,
        on_delete=models.PROTECT
    )




class Corporacao(models.Model):
    nome = models.CharField(
        max_length=100
    )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.