在我的const obj = {
data: {
'Puppies' : [],
'Kittens': []
}
};
const list = [1,2,3,4];
const xlens = R.lensPath(['data', 'Puppies'])
console.log(R.set(xlens, list, obj))
文件中,我从模型中过滤对象,然后向对象添加一些属性,以便稍后在模板中使用它们。
views.py
例如,在我的团队模型中,字段sf,sa,pdo和xpdo不存在。我在视图上计算这些字段,并在我的模板中的表中使用它们。实际上,直到这里才没有问题。我想要做的是将查询集转储为JSON,并将模板传递给JavaScript使用。
matches = Match.objects.filter(season=season_name).order_by("match_date")
team_list = Team.objects.filter(id__in=team_ids)
for team in team_list:
home_matches = matches.filter(home_team=team)
away_matches = matches.filter(away_team=team)
team.sf = len(Shot.objects.filter(match__in=matches, team=team))
team.sa = len(Shot.objects.filter(match__in=matches, opponent_team=team))
team.pdo = ((team.f / team.sf) + (1 - (team.a / team.sa))) * 1000
team.xpdo = ((team.xgf / team.sf) + (1 - (team.xga / team.sa))) * 1000
当我运行服务器时,出现以下错误:
context["teams_json"] = json.dumps(list(team_list.values_list(
"team_name", "pdo", "xpdo")), cls=DjangoJSONEncoder)
我看到这些选择是FieldError at /leagues/turkey2017/
Cannot resolve keyword 'pdo' into field. Choices are: away_team, home_team, id, opponent_team, passmaplineup, passmappass, player_stats_opponent_team, player_stats_team, shooter_team, team_dark_color, team_light_color, team_name
模型中原始字段和其他模型中Team
字段的组合,例如ForeignKey
模型中的此字段:
Shot
如何在视图中稍后再次使用视图中添加的字段?
答案 0 :(得分:0)
问题在于您尝试在不是字段的内容上使用values_list
。我想你这样做的原因是从序列化数据中删除不必要的字段。但是真的在这里使用values_list是过度的;你只需要三位数据,就可以轻松地手动转储它们:
data = [{'pdo': team.pdo, 'xpdo': team.xpdo, 'team_name': team.team_name} for team in team_list]
context["teams_json"] = json.dumps(data)