Django 无法更新模型
我有以下函数来覆盖模型匹配中的默认保存函数
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其添加到您的 admin.py 中
add this in ur admin.py
你的方法显然没有什么问题。因此,通常的调试技巧适用:您确定该方法确实被调用了吗?您确定 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.