nashr rafeeg
nashr rafeeg

Reputation: 829

Django unable to update model

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 ?

Upvotes: 0

Views: 339

Answers (2)

ha22109
ha22109

Reputation: 8336

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()

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599956

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.

Upvotes: 0

Related Questions