How to call an attribute of the class in another class in django

1

I'm trying to create some classes that will relate in a way. The idea is to be a game in which the player will have some options of choice and then his score is based on the punctuation of his choices (rather than a fantasy game).

For example:

class PrimeiroConjunto(models.Model):
    nome = models.CharField(max_length=255, null=False)
    pontuacao = models.FloatField(max_length=4, null=False)

class SegundoConjunto(models.Model):
    nome = models.CharField(max_length=255, null=False)
    pontuacao = models.FloatField(max_length=4, null=False)

class TerceiroConjunto(models.Model):
    nome = models.CharField(max_length=255, null=False)
    pontuacao = models.FloatField(max_length=4, null=False)

class Perfil(models.Model):
    nome = models.CharField(max_length=255, null=False)
    primeiraEscolha = models.ForeignKey(PrimeiroConjunto)
    segundaEscolha = models.ForeignKey(SegundoConjunto)
    terceiraEscolha = models.ForeignKey(TerceiroConjunto)

Then the Profile class would have a relationship with 3 other classes (which have different elements from each other). And it would be necessary to access the punctuation of each choice.

I do not know how to relate these classes by adding direct to the bank, for example, let's say that in the First Set I have 3 elements:

Peter - 100 Points

Alvaro - 70 Points

Bruno - 150 Points

And now I want to add a profile in the bank that goes to the First Set's choice would be Alvaro for example, how can I do this?

Because the only way I know of adding an item to the database is manually through the command prompt, for example:

>>>from abc.models import Perfil
>>>perfil = Perfil(nome='Gabriel', ???)
>>>perfil.save()

So, doing this, what would I put in place of the "???" to associate Gabriel's profile with Alvaro of class PrimeiroConjunto , to access his score,

    
asked by anonymous 28.01.2018 / 17:35

1 answer

1

You can place the instance for the object itself. Example:

>>> from abc.models import Perfil, PrimeiroConjunto
>>> conjunto = PrimeiroConjunto.objects.get(nome='Alvaro')
>>> perfil = Perfil(nome='Gabriel', primeiraEscolha=conjunto)
>>> perfil.save()

You can also add items to the database using views , it does not have to be prompt . But then they will usually be associated with forms ( forms ). You can find more details here .

    
28.01.2018 / 19:54