Classes within classes (Python / Django)

1

Hello, I'm starting to study about python because of Django. I have a doubt that many may consider trivial but I do not know the answer.

I'm building a Template 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         =   ???????????

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 a "Parent" object and a "Mae" within the "Candidate" object?

Thank you in advance for the help of anyone who can help me.

    
asked by anonymous 07.07.2017 / 19:11

2 answers

1

You are looking for a Many-to-one link, 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 templates here

    
07.07.2017 / 19:21
0

Realize that entities: parent, parent and candidate have several attributes in common. To decrease code you can use the abstract django templates.

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 scribbled less code and if any attribute needs to be renamed it will only change in one place

  • I am not aware of standards and good practices in data modeling.
18.01.2018 / 14:52