1

I have two models Betslip and Bet,I want to update value of Betslip.settled_status to 'Lost', if Bet.settlement_status is 'half_lost' or 'lost', can anyone create a method for it ?

class Betslip(models.Model):
    SETTLED_STATUS_CHOICE = (
        ('InGame','InGame'),
        ('Won','Won'),
        ('Lost','Lost'),
        ('Refunded','Refunded')
    )
    settled_status = models.CharField(choices=SETTLED_STATUS_CHOICE,max_length=30)


class Bet(models.Model):
    SETTLEMENT_STATUS_CHOICE = (
        ('','select'),
        ('won','won'),
        ('lost','lost'),
        ('refund','Refund'),
        ('half_won','half_won'),
        ('half_lost','half_lost'),
    )

  
    settlement_status = models.CharField(choices=SETTLEMENT_STATUS_CHOICE,max_length=30,)
    betslip = models.ForeignKey(Betslip,on_delete=models.CASCADE,related_name='bet')```

2 Answers2

2

you can understand about @property from this discussion and for using the bets object for getting bets' settlement status property you may use "reverse related object lookup" aka _set inside the Betlip property(since you have used foreign key for Betslip in Bet)

anonymous
  • 36
  • 1
0

you can do something like this:

In Bet model try this:

def update_settled_status(self):
    if self.settlement_status in ['lost','half_won']:
        self.betslip.settled_status = 'Lost'
        self.betslip.save()
AlexZ
  • 113
  • 6
  • I am not getting any error but it is also not changing value of betslip.settled_status, i ran migrations too.. – Shailendra Singh Aug 07 '21 at 10:21
  • I don't know exactly where you wanna use this. you can call this method like this: bet = Bet.objects.get(pk=_a_bet_pk) bet.update_settled_status() it will save the value with 'self.betslip.save()' in the method declaration. – AlexZ Aug 07 '21 at 11:04
  • I need to override the save method of **Betslip** to save new value in database, how can i call this function in save of Betslip? – Shailendra Singh Aug 09 '21 at 05:40
  • You can do this in **Betslip** save method: `self.bet.first().update_settled_status()` – AlexZ Aug 09 '21 at 23:05