我有以下功能来覆盖模型匹配中的默认保存功能
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)
我能够很好地保存匹配模型,但Team模型似乎根本没有更新,并且没有抛出任何错误。我在这里错过了一些东西吗?
答案 0 :(得分:1)
在你的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()
答案 1 :(得分:0)
你的方法没有明显的错误。所以通常的调试技巧适用:你确定该方法实际上被调用了吗?你确定Match对象的Match_Status是F还是D?请放入一些打印陈述以确定。