1👍
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=Transaction)
def change_account_amt(sender, instance, created, **kwargs):
if created:
# instance is a Transaction created object ...
acc_obj = Account.objects.get(...)
acc_obj.amt = ...
acc_obj.save()
0👍
You can do that easily via a post_save
signal. It is sent at the end of the save()
method.
from django.db.models.signals import post_save
class Transaction(models.Model):
....
class Account(models.Model):
...
# method for updating account amount
def update_account_amount(sender, **kwargs):
instance = kwargs['instance']
created = kwargs['created']
raw = kwargs['raw']
if created and not raw:
account = Account.objects.get(...) # get the account object
account.amt -= instance.amt # update the amount
account.save() # save the account object
# register the signal
post_save.connect(update_account_amount, sender=Transaction)
- Is it safe to save Django secret variables in the settings.py on Heroku?
- How to read Certification details in django Request Object?
Source:stackexchange.com