我正试图把每场足球比赛的分数整理成一个球队在整个赛季中得分的单子。
类似{1,3,3,3,1,.}
我的模型是:
class MatchScores(models.Model):
team = models.ForeignKey(Team, on_delete=models.CASCADE, blank=True)
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, blank=True)
game = models.ForeignKey('game', on_delete=models.CASCADE, blank=True)
points = models.SmallIntegerField(blank=True, null=True)
...
def __str__(self):
return str(self.team)
class Meta:
verbose_name = "Match Scores"
<QuerySet [<MatchScores: Red Team>, <MatchScores: Blue Team>,... >]添加到模型中的数据不符合要求,例如:
Red Team 1 ...
Red Team 3 ...
Blue Team 1 ...
Gren Team 1 ...
Red Team 3...所以我不知道我是如何为每支球队收集每一分并将其组合在一起的。
我试过了
points = MatchScores.objects.filter(profile__user=profile.user)
all_points = []
for team in points:
if "points" in team:
all_points.append(team["points"])但这会返回TypeError: argument of type MatchScores' is not iterable
发布于 2022-09-25 08:31:36
不能迭代对象,因此行:if "points" in team:无效。要得到一个对象的字段,不要把它当作字典来处理。对于对象,则使用.。改为:
for team in points:
if team.points: # it will be False if points == 0
all_points.append(team.points)https://stackoverflow.com/questions/73842934
复制相似问题