有可能吗?我想比较两个字段并将得分返回到bet_score字段。
class Tabela(models.Model):
score1= models.IntegerField(max_length=3)
score2 = models.IntegerField(max_lenght=3)
bet_score= models.IntegerField(defaults=bet_score())
def bet_score(self):
if (self.score1> self.score2 ):
return 1
elif (self.score1== self.score2 ):
return 0
else:
return 2
答案 0 :(得分:0)
如果你想在模型上存储这类东西,我认为你最好的选择是覆盖保存方法:
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)
查看the docs了解详情。