每次我的投注模型保存时,我都会尝试更新我的游戏模型的模型实例。每次我下注保存时,我都想在游戏模型的选择字段中添加1。如果我在游戏中打印出选择中保存覆盖,看起来它会访问并更新数字,但它不会将其保存到数据库中。
我的假设是,这是因为我还要压倒游戏方法,但是当我发表评论时,它仍然无效。在游戏模型上保存的覆盖非常长,所以如果你在那里看到错误,请告诉我。
我也最终尝试了post_save信号并最终得到了同样的错误。该代码也在下面。
如果您有任何建议,请告诉我们。
投注模型:
class Bet(BaseModel):
""""
Bets on team games
"""
size = models.IntegerField()
player = models.ForeignKey('bets.PlayerProfile', null=True, blank=True, related_name='bets')
team_game = models.ForeignKey('betting.TeamGame')
spread_at_time = models.ForeignKey('betting.Spread', related_name="spread_bets", blank=True, null=True)
moneyline_at_time = models.ForeignKey('betting.Moneyline', blank=True, null=True, related_name="moneyline_bets")
winner = models.BooleanField(default=False)
tag = models.CharField(blank=True, null=True, max_length=128)
# using char field to capture push
outcome = models.CharField(blank=True, null=True, max_length=128)
pro = models.BooleanField(default=False)
cost = models.IntegerField(default=0)
#for pro description
description = models.CharField(max_length=500, null=True, blank=True)
def save(self, *args, **kwargs):
super(Bet, self).save(*args, **kwargs)
# Update the number of picks to the game
game = self.team_game.game
game.picks += 1
game.save()
# Update the number of picks to the team_game
team_game = self.team_game
team_game.picks += 1
# calc team pick percentage
game_picks = self.team_game.game.picks
team_game_picks = self.team_game.picks
team_game.pick_pct = float(team_game_picks) / game_picks
team_game.save()
游戏模型:
class Game(BaseModel):
""""
Game type that holds identification, name and status
"""
name = models.CharField(max_length=200)
picks = models.IntegerField(default=0)
players = models.ManyToManyField('bets.PlayerProfile')
teams = models.ManyToManyField('betting.Team', through='betting.TeamGame')
final = models.BooleanField(default=False)
identification = models.CharField(max_length=500, blank=True, null=True, unique=True)
sport = models.ForeignKey('betting.Sport', blank=True, null=True)
time = models.DateTimeField(blank=True, null=True)
def __str__(self):
return self.identification
def save(self, *args, **kwargs):
super(Game, self).save(*args, **kwargs)
if self.final:
# finds teams that bet on games
teams = self.teamgame_set.all()
try:
team_a = teams[0]
team_b = teams[1]
# scores with spread into account
final_a_score = team_a.score - team_a.spreads.last().value
final_b_score = team_b.score - team_b.spreads.last().value
# Get all the bets in this game
bets_on_team_a = Bet.objects.filter(team_game__game=self, team_game__team=team_a)
bets_on_team_b = Bet.objects.filter(team_game__game=self, team_game__team=team_b)
for bet in bets_on_team_a:
# check spread first
if bet.tag == "Spread":
if team_a.score - bet.spread_at_time.value > team_b.score:
bet.winner = True
bet.outcome = "Win"
record = Record.objects.get_or_create(player=bet.player)[0]
record.profit += bet.size
record.balance += bet.size
record.wins += 1
if record.streak >= 0:
record.streak += 1
else:
record.streak = 1
if team_a.score - bet.spread_at_time.value < team_b.score:
bet.outcome = "Loss"
record = Record.objects.get_or_create(player=bet.player)[0]
record.profit -= bet.size
record.balance -= bet.size
record.losses += 1
if record.streak <=0:
record.streak-=1
else:
record.streak = -1
if team_a.score - bet.spread_at_time.value == team_b.score:
bet.outcome = "Push"
record.pushes += 1
else:
if team_a.score > team_b.score:
bet.winner = True
bet.outcome = "Win"
record.wins += 1
if record.streak >= 0:
record.streak += 1
else:
record.streak = 1
if team_a.score < team_b.score:
bet.outcome = "Loss"
record.losses += 1
if record.streak <=0:
record.streak-=1
else:
record.streak = -1
if team_a.score == team_b.score:
bet.outcome = "Push"
record.pushes += 1
bet.save()
record.save()
for bet in bets_on_team_b:
if bet.tag == "Spread":
if team_b.score - bet.spread_at_time.value > team_a.score:
bet.winner = True
bet.outcome = "Win"
record = Record.objects.get_or_create(player=bet.player)[0]
record.profit += bet.size
record.balance += bet.size
record.wins += 1
if record.streak >= 0:
record.streak += 1
else:
record.streak = 1
if team_b.score - bet.spread_at_time.value < team_a.score:
bet.outcome = "Loss"
record = Record.objects.get_or_create(player=bet.player)[0]
record.profit -= bet.size
record.balance -= bet.size
record.losses += 1
if record.streak <=0:
record.streak-=1
else:
record.streak = -1
if team_b.score - bet.spread_at_time.value == team_a.score:
bet.outcome = "Push"
record.pushes += 1
# moneyline bet
else:
if team_b.score > team_a.score:
bet.winner = True
bet.outcome = "Win"
record = Record.objects.get_or_create(player=bet.player)[0]
# calc moneyline multiplier
multiplier = (bet.moneyline_at_time.value / 100)
record.profit += multiplier * bet.size
record.balance += multiplier * bet.size
record.wins += 1
if record.streak >= 0:
record.streak += 1
else:
record.streak = 1
if team_b.score < team_a.score:
bet.outcome = "Loss"
record.losses += 1
if record.streak <=0:
record.streak-=1
else:
record.streak = -1
if team_b.score == team_a.score:
bet.outcome = "Push"
record.pushes += 1
bet.save()
record.save()
发布保存信号:
@receiver(post_save, sender=Bet)
def my_callback(sender, instance, **kwargs):
team_game = instance.team_game
game = team_game.game
game.picks += 1
game.save()