在django / python复制条目中保存函数

时间:2016-04-20 21:30:16

标签: python django django-models django-forms django-views

我正在使用与NFL比赛日数据库相同的架构为本地联赛编写足球得分应用程序。我创建了一个最终运行的功能,它自己更新每个玩家的分数。

问题出现在运行创建新点记录的功能时,它会复制每个玩家的条目,没有显示错误或任何内容,除重复值外,一切都按预期运行。

这是我的views.py:

def updatepoints(request):
    actual = get_object_or_404(CurrentWeek, status='active')
    week = actual.week
    season = actual.season
    ptsexist = Puntos.objects.filter(week=week, season=season)
    if ptsexist:
        pts = Player.objects.raw('''SELECT DISTINCT player.player_id,(SELECT (SELECT SUM(play_player.passing_yds))+(SELECT SUM(play_player.passing_tds))+(SELECT SUM(play_player.passing_twoptm))+(SELECT SUM(play_player.passing_int))+(SELECT SUM(play_player.rushing_yds))+(SELECT SUM(play_player.rushing_tds))+(SELECT SUM(play_player.rushing_twoptm))+(SELECT SUM(play_player.fumbles_lost))+(SELECT SUM(play_player.receiving_yds))+(SELECT SUM(play_player.receiving_tds))+(SELECT SUM(play_player.receiving_twoptm))+(SELECT SUM(play_player.receiving_rec))+(SELECT SUM(play_player.kicking_fgm))+(SELECT SUM(play_player.kicking_xpmade))+(SELECT SUM(play_player.fumbles_rec_tds))+(SELECT SUM(play_player.kicking_rec_tds))) AS total,id_puntos FROM player INNER JOIN play_player ON player.player_id = play_player.player_id INNER JOIN game ON play_player.gsis_id = game.gsis_id LEFT JOIN points ON player.player_id = points.player_id AND points.temporada = game.season_year AND "DraftFantasy_puntos".semana = game.week WHERE game.week = %s AND game.season_year = %s AND game.season_type != 'Warmup' AND game.season_type != 'Postseason' GROUP BY player.player_id,points.id_points''', [week, season])
        for obj in pts:
            obj.id = obj.player_id
            obj.points = obj.total
            obj.idpoints = obj.id_points
            form = UpdatePointsForm(request.POST)
            pointsf = form.save(commit=False)
            pointsf.id_points = obj.idpoints
            pointsf.player_id = obj.player_id
            pointsf.temporada = season
            pointsf.semana = week
            pointsf.puntos_ppr = obj.total
            pointsf.save()
        return HttpResponseRedirect("/dashboard/")
    else:
        return HttpResponseRedirect("/savepoints/")

def savepoints(request):
    actual = get_object_or_404(CurrentWeek, status='active')
    week = actual.week
    season = actual.season
    ptsn = Player.objects.raw('''SELECT DISTINCT player.player_id,(SELECT (SELECT SUM(play_player.passing_yds))+(SELECT SUM(play_player.passing_tds))+(SELECT SUM(play_player.passing_twoptm))+(SELECT SUM(play_player.passing_int))+(SELECT SUM(play_player.rushing_yds))+(SELECT SUM(play_player.rushing_tds))+(SELECT SUM(play_player.rushing_twoptm))+(SELECT SUM(play_player.fumbles_lost))+(SELECT SUM(play_player.receiving_yds))+(SELECT SUM(play_player.receiving_tds))+(SELECT SUM(play_player.receiving_twoptm))+(SELECT SUM(play_player.receiving_rec))+(SELECT SUM(play_player.kicking_fgm))+(SELECT SUM(play_player.kicking_xpmade))+(SELECT SUM(play_player.fumbles_rec_tds))+(SELECT SUM(play_player.kicking_rec_tds))) AS total FROM player INNER JOIN play_player ON player.player_id = play_player.player_id INNER JOIN game ON play_player.gsis_id = game.gsis_id WHERE game.week = %s AND game.season_year = %s AND game.season_type != 'Warmup' AND game.season_type != 'Postseason' GROUP BY player.player_id''', [week, season])
    for obj in ptsn:
        obj.id = obj.player_id
        obj.points = obj.total
        formn = PointsForm(request.POST)
        pointsfn = formn.save(commit=False)
        pointsfn.player_id = obj.player_id
        pointsfn.temporada = season
        pointsfn.semana = week
        pointsfn.points = obj.total
        pointsfn.save()
    return HttpResponseRedirect("/ligas/")

forms.py:

class PointsForm(forms.ModelForm):
    class Meta:
        model = Points
        exclude = ["player_id",
                   "season",
                   "week",
                   "puntos"]

class UpdatePointsForm(forms.ModelForm):
    class Meta:
        model = Points
        exclude = ["id_points",
                   "player_id",
                   "season",
                   "week",
                   "points"]

和models.py:

class Points(models.Model):
    id_points = models.AutoField(primary_key=True, null=False, max_length=15)
    player_id = models.CharField(max_length=100)
    season = models.IntegerField(max_length=10)
    week = models.IntegerField(max_length=10)
    puntos = models.IntegerField(max_length=50)

class CurrentWeek(models.Model):
    id_week = models.AutoField(primary_key=True, null=False, max_length=15)
    week = models.IntegerField(max_length=10)
    season = models.IntegerField(max_length=5)
    status = models.CharField(max_length=50, default="done")

我真的很难过,所以任何帮助都会受到高度赞赏。

0 个答案:

没有答案