3

I've a simple signals which is launch when the object is deleted

@receiver(post_delete, sender=Operation, dispatch_uid="delete_operation")
def delete_operation(sender, instance, **kwargs):
    # Track balance operations for delete
    instance.user.balance = instance.user.balance - int(instance.amount) # Note the "-"
    instance.user.save()

The problem is that when I delete the object into the admin, the signals is launch twice and false my balance. I would like to avoid the 2 times trigger. I searched on stackoverflow and tried several solutions but still not working.

Here is how I import the signals :

apps.py

class CommonConfig(AppConfig):
name = 'sl.common'

def ready(self):
    import sl.common.signals

If anyone has ideas it's welcome !!

Community
  • 1
  • 1
DaschPyth
  • 195
  • 1
  • 2
  • 9
  • Have you tried pre_delete? – trantu May 04 '16 at 17:06
  • Where does this code reside? How is it included in your application? –  May 04 '16 at 17:22
  • I didn't try pre_delete since we need post_delete. this code resides in the core app. first one is in common.signals. the lonely problem is that this code works but is launched twice in admin. so that's bad – DaschPyth May 09 '16 at 08:31

1 Answers1

0
@receiver(post_delete, sender=Operation, dispatch_uid="delete_operation")
def delete_operation(sender, instance, action, **kwargs):
    if action[0:4] == 'post':
        # Track balance operations for delete
        instance.user.balance = instance.user.balance - int(instance.amount) # Note the "-"
        instance.user.save()

Each signal has two actions: pre and post - pre instance has fields before changes, post instance has fields after changes.

  • pre_add and post_add
  • pre_remove and post_remove
Tomasz Brzezina
  • 1,452
  • 5
  • 21
  • 44