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.
– Christiano Ribeiro Soares