Saving past data - Django

0

My question is as follows, in my application I have a Profile template that has the following fields:

Models.py:

class Perfil(models.Model):
    nome = models.CharField(max_length=255, null=False)
    pontuacao_total = models.FloatField(max_length=4, default=0)
    pontuacao_ultima = models.FloatField(max_length=4, default=0)

In this application, there will be several rounds of plays, in this modeling, each round will have its score associated with the field pontuacao_ultima , and pontuacao_total will be the sum of these scores. For example:

Round 1: 10 points

Round 2: 15 points

In this case, pontuacao_ultima will be equal to 15 and pontuacao_total equal to 25.

But in this way of approaching the situation there is a big problem that is the loss of data, I will only have stored the total and last scores of each player.

So I wanted to ask for a suggestion for some solution in this case, what can I do to not lose this data? For what I researched, there is no possibility of a template field being a list that would store the value of each round ...

Can anyone give me a light?

    
asked by anonymous 02.04.2018 / 00:49

1 answer

4

You need to create a template to store the Rounds, something like:

class Rodada(models.Model):
    jogador = models.ForeignKey(Perfil, on_delete=models.CASCADE, related_name='rodadas')
    rodada = models.IntegerFileld()
    pontos = models.FloatField(max_length=4, default=0)

Then you do not need to store punctuations in the user profile to access this information.

Then create two methods within the Profile class to retrieve the score of the highest round and another to add up the score of a given user.

class Perfil(models.Model):
    nome = models.CharField(max_length=255, null=False)

    @property
    def pontuacao_total(self)
        total = 0
        if hasattr(self,'rodadas'):
           for rodada in self.rodadas.all()
               total += rodada.pontos
        return total

    @property
    def pontuacao_ultima(self)
        ultima = 0
        if hasattr(self, 'rodadas'):
            ultima = self.rodadas.last().pontos
        return ultima
    
02.04.2018 / 02:48