Classes within classes (Python/Django)

Asked

Viewed 233 times

1

Ola, I’m starting to study python on account of Django. I have a question that many may consider trivial but I don’t know the answer.

I am building a Model with the following features:

class Candidato(models.Model):
   nome        =   models.CharField(max_length=300, required=True)
   mail        =   models.EmailField(max_length=300, required=True)
   fone        =   models.CharField(max_length=20, required=True)
   nascimento  =   models.DateTimeField(blank=False, null=False)
   cpf         =   models.CharField(max_length=50, required=True)
   rg          =   models.CharField(max_length=50, required=True)
   trabalho    =   models.EmailField(max_length=300, required=True)
   mae         =   ???????????
   pai         =   ???????????

And I need the fields "mother" and "father" to be two objects with the following structure:

class Mae(models.Model):
   nome        =   models.CharField(max_length=300, required=True)
   nascimento  =   models.DateTimeField(blank=False, null=False, required=True)
   cpf         =   models.CharField(max_length=50, required=True)
   rg          =   models.CharField(max_length=50, required=True)
   mail       =   models.EmailField(max_length=300, required=True)
   fone        =   models.CharField(max_length=20, required=True)
   trabalho    =   models.EmailField(max_length=300, required=True)

class Pai(models.Model):
   nome        =   models.CharField(max_length=300, required=True)
   nascimento  =   models.DateTimeField(blank=False, null=False, required=True)
   cpf         =   models.CharField(max_length=50, required=True)
   rg          =   models.CharField(max_length=50, required=True)
   email       =   models.EmailField(max_length=300, required=True)
   fone        =   models.CharField(max_length=20, required=True)
   trabalho    =   models.EmailField(max_length=300, required=True)
   membro      =   models.BooleanField()

How do I reference an object "Father" and a "Mother" within the object "Candidate"?

I am grateful for the help of those who can help me.

2 answers

1


You’re looking for a relationship Many-to-one, see:

class Mae(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

class Pai(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

class Candidato(models.Model):
    mae = models.ForeignKey(Mae, on_delete=models.CASCADE)
    pai = models.ForeignKey(Pai, on_delete=models.CASCADE)

See more about the different types of fields in the models here

  • I will read about this type of relationship. Thank you very much for your help :D

  • Welcome to Stack Overlfow! : ) If the answer has solved your problem, don’t forget to mark as accepted.

0

Notice that the entities: father, mother and candidate have several attributes in common. For code reduction you can use the abstract models of Django.

class Pessoa(models.Model):
  nome = models.CharField(max_length=300, required=True)
  nascimento = models.DateTimeField(blank=False,null=False,required=True)
  cpf = models.CharField(max_length=50, required=True)
  rg = models.CharField(max_length=50, required=True)
  mail = models.EmailField(max_length=300, required=True)
  fone =  models.CharField(max_length=20, required=True)
  trabalho = models.EmailField(max_length=300, required=True)

  class Meta:
     abstract = True

 class Pai(Pessoa):
     # algum Atributo para Pai

 class Mae(Pessoa):
     # algum atributo para Mãe

 class Candidato(Pessoa):
     mae = models.ForeignKey(Mae, on_delete=models.CASCADE)
     pai = models.ForeignKey(Pai, on_delete=models.CASCADE)

So Django will not create the Person table and you have scrutinized less code and if any attribute needs to change name just change in one place

  • I am not taking into account data modeling standards and best practices.

Browser other questions tagged

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