0

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
tomoc4
  • 337
  • 2
  • 10
  • 29

2 Answers2

1

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()
Ahtisham
  • 9,170
  • 4
  • 43
  • 57
  • @tomoc4 Why can you not override save method as I showed ? – Ahtisham Feb 04 '19 at 13:45
  • I have tried your solution but I am getting NameError: name 'Model' is not defined – tomoc4 Feb 04 '19 at 14:07
  • @tomoc4 Try it I edited the answer. Sorry it should have been `testModel` instead of `Model` – Ahtisham Feb 04 '19 at 14:08
  • thank you. your solution worked. I'm planning to add a lot of functions to be called on different states of model fields would signals be a better approach? my background tasks save every two seconds so I would like to avoid over riding the save method as little as possible. This will certainly help my logic for now tho. Thanks again! – tomoc4 Feb 04 '19 at 14:16
  • yes overriding save method would be better approach because it would be lot easy to debug your program as everything related to `testModel` would be under save method. – Ahtisham Feb 04 '19 at 14:20
0

Yes you can use signals for that. In your case you can get the update status from the instance.

@receiver(post_save, sender=testModel)
def post_save(sender, instance, created, **kwargs):
    if instance.status == 'inplay':
       send()
  • how can I check if this works easily? I'm editing the field in django admin but I think my send function isn't working so I don't know if the answer is working correctly – tomoc4 Feb 04 '19 at 13:26