Django 无法更新模型

发布于 2024-08-24 14:30:47 字数 805 浏览 3 评论 0原文

我有以下函数来覆盖模型匹配中的默认保存函数

def save(self, *args, **kwargs):
   if self.Match_Status == "F":
        Team.objects.filter(pk=self.Team_one.id).update(Played=F('Played')+1)
        Team.objects.filter(pk=self.Team_two.id).update(Played=F('Played')+1)
        if self.Winner !="":   
          Team.objects.filter(pk=self.Winner.id).update(Win=F('Win')+1, Points=F('Points')+3)
        else:
            return
   if self.Match_Status == "D":
        Team.objects.filter(pk=self.Team_one.id).update(Played=F('Played')+1, Draw = F('Draw')+1, Points=F('Points')+1)
      Team.objects.filter(pk=self.Team_two.id).update(Played=F('Played')+1, Draw = F('Draw')+1, Points=F('Points')+1)
   super(Match, self).save(*args, **kwargs)

我能够很好地保存匹配模型,但团队模型似乎根本没有更新,也没有抛出任何错误。我在这里错过了什么吗?

i have the following function to override the default save function in a model match

def save(self, *args, **kwargs):
   if self.Match_Status == "F":
        Team.objects.filter(pk=self.Team_one.id).update(Played=F('Played')+1)
        Team.objects.filter(pk=self.Team_two.id).update(Played=F('Played')+1)
        if self.Winner !="":   
          Team.objects.filter(pk=self.Winner.id).update(Win=F('Win')+1, Points=F('Points')+3)
        else:
            return
   if self.Match_Status == "D":
        Team.objects.filter(pk=self.Team_one.id).update(Played=F('Played')+1, Draw = F('Draw')+1, Points=F('Points')+1)
      Team.objects.filter(pk=self.Team_two.id).update(Played=F('Played')+1, Draw = F('Draw')+1, Points=F('Points')+1)
   super(Match, self).save(*args, **kwargs)

I am able to save the match model just fine but Team model does not seem to be updating at all and no error is being thrown. am i missing some thing here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东走西顾 2024-08-31 14:30:47

将其添加到您的 admin.py 中

def save_model(self, request ,obj ,form,change):
   if obj.Match_Status == "F":
    Team.objects.filter(pk=obj.Team_one.id).update(Played=F('Played')+1)
    Team.objects.filter(pk=obj.Team_two.id).update(Played=F('Played')+1)
    if obj.Winner !="":   
      Team.objects.filter(pk=obj.Winner.id).update(Win=F('Win')+1, Points=F('Points')+3)
    else:
        return
     if obj.Match_Status == "D":
    Team.objects.filter(pk=obj.Team_one.id).update(Played=F('Played')+1, Draw = F('Draw')+1, Points=F('Points')+1)
      Team.objects.filter(pk=obj.Team_two.id).update(Played=F('Played')+1, Draw = F('Draw')+1, Points=F('Points')+1)
     obj.save()

add this in ur admin.py

def save_model(self, request ,obj ,form,change):
   if obj.Match_Status == "F":
    Team.objects.filter(pk=obj.Team_one.id).update(Played=F('Played')+1)
    Team.objects.filter(pk=obj.Team_two.id).update(Played=F('Played')+1)
    if obj.Winner !="":   
      Team.objects.filter(pk=obj.Winner.id).update(Win=F('Win')+1, Points=F('Points')+3)
    else:
        return
     if obj.Match_Status == "D":
    Team.objects.filter(pk=obj.Team_one.id).update(Played=F('Played')+1, Draw = F('Draw')+1, Points=F('Points')+1)
      Team.objects.filter(pk=obj.Team_two.id).update(Played=F('Played')+1, Draw = F('Draw')+1, Points=F('Points')+1)
     obj.save()
萌逼全场 2024-08-31 14:30:47

你的方法显然没有什么问题。因此,通常的调试技巧适用:您确定该方法确实被调用了吗?您确定 Match 对象的 Match_Status 为 F 或 D 吗?添加一些打印语句以确保确定。

There's nothing obviously wrong with your method. So usual debugging tricks apply: are you sure the method is actually being called? Are you sure the Match object has a Match_Status of either F or D? Put in some print statements to be sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文