如何处理django-debug-toolbar提示SQL查询重复?

Model.py:

class Match(models.Model):

home_team = models.CharField(max_length=200)

away_team = models.CharField(max_length=200)

class Stat(models.Model):

match = models.ForeignKey(Match)

team = models.CharField(max_length=100)

goals = models.IntegerField(default=0)

assists = models.IntegerField(default=0)

views.py

    context_dict = {}

match = Match.objects.get(pk=1)

home_stat = Stat.objects.get(match=match, team=match.home_team)

away_stat = Stat.objects.get(match=match, team=match.away_team)

context_dict['home_stat'] = home_stat

context_dict['away_stat'] = away_stat

return render(request, 'index.html', context_dict)

template:

goals: {{ home_stat.goals }} : {{ away_stat.goals }}

assists: {{ home_stat.assists }} : {{ away_stat.assists }}

django-debug-toolbar截图:
图片描述

请问怎样修改才不会有重复查询?谢谢。

以上是 如何处理django-debug-toolbar提示SQL查询重复? 的全部内容, 来源链接: utcz.com/a/157870.html

回到顶部