0

I'm using Django 2.x

I have a model TransactionLog which logs the amount given and updated. Each time amount is given, a new record is created in AmountGiven model and thus will create a log with action as given and if the earlier given amount is updated, it will log with action as updated.

@receiver(post_save, sender=AmountGiven)
def amount_given_post_save_receiver(sender, instance, created, **kwargs):
    if created:
        action = 'given'
    else:
        action = 'updated'

    TransactionLog.objects.create(
        user=instance.contact.user,
        contact=instance.contact,
        amount_given=instance,
        amount=instance.amount,
        action=action
    )

But even when a new record is created, post save receiver is being called two or more than two times. Then, one record is saved as action=given in TransactionLog and others are creating action=updated record in the TransactionLog model.

How can I make it unique to each call?

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

0 Answers0