Set attribute of another Python class

Asked

Viewed 84 times

0

I am studying python and would like to understand how I can define an attribute of a class based on attributes of another class.

I’ll go to the example to be clearer, it’s just an example of what I need for my small application Jango, where in the register of a person, I will set her position based on job registrations.

    {class Pessoa(models.Model):

    nome = models.CharField(max_length=20, null=False)
    cargo = ##abrir opções de Cargos cadastrados em "Cargos"


class Cargos(models.Model):

    nome_cargo = models.CharField(max_length=20, null=False)}
  • Here is an explanation of Django’s relationships: Relationships from DJANGO Table

  • You want a class in another class?

  • 1

    @Maurydeveloper hi, I managed to resolve, actually it was a relationship, I answered the question because I could understand here, Thank you

1 answer

3


Person should have a relationship with Cargo, which in this case is a 1,n relationship. Django uses Foreignkey for this type of relationship.

class Pessoa(models.Model):
    nome = models.CharField(max_length=20, null=False)
    cargo = models.ForeignKey('Cargos', on_delete=models.CASCADE)


class Cargos(models.Model):
    nome_cargo = models.CharField(max_length=20, null=False)}

Browser other questions tagged

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