我有一个使用Django
框架编写的世界杯预测网站。每次用户请求查看特定比赛的站立表时,standing_stable视图都会查找与该比赛相关的所有用户,然后加载所有相关的游戏以及用户的预测,然后在for循环中进行比较将预测结果与实际结果进行比较,并根据规则系统分配分数。根据我的经验,当我有10个以上的用户注册了至少有30场或更多游戏的比赛时,stanting_table视图的速度就会变慢,加载表需要5-10秒钟以上的时间。
我知道我这样做的方式效率不高,我宁愿在每场比赛结束后一劳永逸地进行数学运算,并且可以将这些预制的站立桌子推荐给用户。但是,我不知道如何将此类表存储为Django
模型。
这是我的show_standing
视图
@login_required
def show_standing(request, contest):
contest = request.user.contests.filter(name=contest).all()[0]
users = contest.users.all()
rows = []
for user in users:
exact_groupstage = utils.get_correct_predictions(user, contest, 'exact', 'groupstage')
goal_difference_groupstage = utils.get_correct_predictions(user, contest, 'goal-difference', 'groupstage')
winner_only_groupstage = utils.get_correct_predictions(user, contest, 'winner-only', 'groupstage')
exact_playoffs = utils.get_correct_predictions(user, contest, 'exact', 'playoffs')
goal_difference_playoffs = utils.get_correct_predictions(user, contest, 'goal-difference', 'playoffs')
winner_only_playoffs = utils.get_correct_predictions(user, contest, 'winner-only', 'playoffs')
row = []
row.append(user)
row.append(exact_groupstage + exact_playoffs)
row.append(goal_difference_groupstage + goal_difference_playoffs)
row.append(winner_only_groupstage + winner_only_playoffs)
row.append(exact_groupstage*3 + goal_difference_groupstage*2 + winner_only_groupstage*1 +
exact_playoffs*6 + goal_difference_playoffs*4 + winner_only_playoffs*2)
rows.append(row)
rows = sorted(rows, key=lambda x:x[4], reverse=True)
data = {'rows': rows,
'contest': contest.name}
return render(request, 'standing.html', data)
get_correct_predictions
的外观如下:
def get_correct_predictions(user, contest, kind, round):
bets = user.bets.filter(contest=contest)
# games = Game.objects.all()
num_exact_predictions = 0
num_goal_dif_predictions = 0
num_winner_only_predictions = 0
for bet in bets:
game = bet.game
if not is_played(game):
continue
if round == 'groupstage':
if game.isplayoff:
continue
elif round == 'playoffs':
if not game.isplayoff:
continue
else:
raise Exception('Round can only be "groupstage" or "playoffs"')
home_predicted = bet.home_score
away_predicted = bet.away_score
home_actual = game.home_score
away_actual = game.away_score
# exact result predicted
if (home_predicted == home_actual) and (away_predicted == away_actual):
num_exact_predictions += 1
# only goal difference predicted
elif (home_predicted - away_predicted) == (home_actual - away_actual):
num_goal_dif_predictions += 1
# only winner predicted
elif (home_predicted - away_predicted) * (home_actual - away_actual) > 0:
num_winner_only_predictions += 1
if kind == 'exact':
return num_exact_predictions
elif kind == 'goal-difference':
return num_goal_dif_predictions
elif (kind == 'winner-only'):
return num_winner_only_predictions
编辑1:这是我的模型:
class Contest(models.Model):
tournament = models.ForeignKey(Tournament)
name = models.CharField(max_length=20)
users = models.ManyToManyField(User, related_name='contests')
class Team(models.Model):
name = models.CharField(max_length=20)
abbreviation = models.CharField(max_length=3)
class Game(models.Model):
tournament = models.ForeignKey(Tournament, related_name='games')
home = models.ForeignKey(Team, related_name='%(class)s_home')
away = models.ForeignKey(Team, related_name='%(class)s_away')
home_score = models.IntegerField()
away_score = models.IntegerField()
isplayoff = models.BooleanField(default=False)
scheduled_datetime = models.DateTimeField()
class Bet(models.Model):
user = models.ForeignKey(User, related_name='bets')
game = models.ForeignKey(Game, related_name='bets')
contest = models.ForeignKey(Contest, related_name='bets')
home_score = models.IntegerField()
away_score = models.IntegerField()
非常感谢您的帮助。