UpdateView以及更新其他模型字段?

时间:2018-06-29 17:38:35

标签: python html django

class FootballUpdate(UpdateView):
    model = Football
    fields = ['date', 'time', 'duration', 'category', 'place', 'game_status', 'event', 'score_team1', 'score_team2', 'team1', 'team2', 'game_level']
    template_name_suffix = '_update_form'
    success_url = reverse_lazy('index')

    def post(self, request, pk):
        match = Football.objects.get(pk=pk)
        team1 = match.team1
        team2 = match.team2
        score_team1 = match.score_team1
        score_team2 = match.score_team2
        if score_team1 > score_team2:
            football_score = FootballScore.objects.filter(team=team1)[0]
            football_score.win = football_score.win + 1
            football_score.save()
            return HttpResponse('Score Successfully Updated! Team1 won!')
        else:
            football_score = FootballScore.objects.filter(team=team2)[0]
            football_score.win = football_score.win + 1
            football_score.save()
            return HttpResponse('Score Successfully Updated! Team2 won!')


------------------------------------------------------------------------

class Football(Match):
    score_team1 = models.IntegerField(default='-1')
    score_team2 = models.IntegerField(default='-1')
    team1 = models.ForeignKey(Team, related_name='team1_football', on_delete=models.CASCADE, null=True)
    team2 = models.ForeignKey(Team, related_name='team2_football', on_delete=models.CASCADE)
    game_level = models.CharField(max_length=256, null=True, choices=LEVEL_CHOICES) # like semi-final, final etc
    # connect = models.ForeignKey(Match, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.game_level)


class FootballScore(models.Model):
    team = models.ForeignKey(Team, related_name='teams', on_delete=models.CASCADE)
    win = models.IntegerField(default='0')

“我的运动是足球”拥有其通常的领域,现在我在足球得分上更新团队的胜利领域。如您所见,我以前已经制作了UpdateView for Football模型,现在我对其进行了更改,以添加增加赢取字段的新要求,以前updateView可以正常工作,添加了“ win”字段之后,我的赢取字段可以很好地增加但提交的更新无效。谢谢。

1 个答案:

答案 0 :(得分:0)

不更新匹配项(Football对象),因为您的视图未保存表单给定的匹配项的更新版本。通常,UpdateView会使用post方法为您做到这一点-通过覆盖该方法,保存更改是您的责任。

def post(self, request, pk):
    match = self.object = self.get_object() # Get the object that this view is working on and set 'self.object' as the ModelForm is going to look up that attribute
    form = self.get_form() # Get the actual ModelForm
    if not form.is_valid():
        # The user may have put in bad data, you wouldn't want to proceed then
        return super().form_invalid(form)
    form.save() # this updates the instance given to the ModelForm via 'self.object' with the data from the form
    team1 = match.team1
    team2 = match.team2
    score_team1 = match.score_team1
    score_team2 = match.score_team2
    if score_team1 > score_team2:
        football_score = FootballScore.objects.filter(team=team1)[0]
        football_score.win = football_score.win + 1
        football_score.save()
        return HttpResponse('Score Successfully Updated! Team1 won!')
    else:
        football_score = FootballScore.objects.filter(team=team2)[0]
        football_score.win = football_score.win + 1
        football_score.save()
        return HttpResponse('Score Successfully Updated! Team2 won!')