[Fixed]-Django model field with function

1๐Ÿ‘

โœ…

If you want to store this kind of thing on a model, I think your best option would be to override the save method:

class Tabela(models.Model):
    score1= models.IntegerField(max_length=3)
    score2 = models.IntegerField(max_lenght=3)
    bet_score= models.IntegerField()

    def save(self, *args, **kwargs):
        if (self.score1 > self.score2 ):
            self.bet_score = 1
        elif (self.score1 == self.score2 ):
            self.bet_score = 0
        else:
            self.bet_score = 2
        super(Tabela, self).save(*args, **kwargs)

Check out the docs for more info.

๐Ÿ‘คdenvaar

Leave a comment