I have a model that's being updated by a background task every few seconds. I would like to execute a function when the instance of the attribute status changes to inplay
I have looked through documentation and examples but can a't find what I'm looking for. Would signals be the best option to call a function after model instance field changes to inplay'?
from django.db import models
class testModel(models.Model):
player1 = models.CharField(null=True, max_length=50)
player2 = models.CharField(null=True, max_length=50)
Player1_odds = models.FloatField(null=True)
Player2_odds = models.FloatField(null=True)
status = models.CharField(null=True, max_length=10)
complete = models.CharField(null=True, max_length=10)
from django.dispatch import receiver
from django.db.models.signals import pre_save, pre_delete, post_save,
post_delete
from django.dispatch import receiver
@receiver(post_save, sender=testModel)
def post_save(sender, instance, created, **kwargs):
# if status is = inplay call send
#
#
pass
def send()
# run bet
You should choice overriding save method rather than signals because your changes are specific to testModel
only. So this is how you would override save method:
class testModel(models.Model):
status = models.CharField(null=True, max_length=10)
# your other model fields
def save(self):
super(testModel, self).save() # this will save model
if self.status == 'inplay':# this will check if the status is "inplay" after change
send()