Storing past data - Django

Asked

Viewed 86 times

0

My question is the following, 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 the pontuacao_total shall be the sum of those scores. For example:

1st Round: 10 points

2nd Round: 15 points

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

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

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

Someone can give me a light?

1 answer

4


You need to create a template to store Spins, 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)

Hence no need to store user profile scores to access this information.

Then create two methods within the Profile class to regain the highest round score and another to add a particular user’s score.

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
  • Great solution by the way.

Browser other questions tagged

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