代码之家  ›  专栏  ›  技术社区  ›  Aadil Hoda

是否在更新其他模型字段的同时更新视图?

  •  0
  • Aadil Hoda  · 技术社区  · 6 年前
    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,现在我正在对它进行修改,以添加我的新要求,增加win field,以前updateview工作正常,添加“win”field后,我的win field在增加fine,但update field不工作。谢谢。

    1 回复  |  直到 6 年前
        1
  •  0
  •   CoffeeBasedLifeform    6 年前

    火柴 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!')