Django:我可以为同一操作执行两个预保存信号吗?

Django: Can I perform two pre-save signal for same operation?

我能否在 django 中为相同的操作执行两个预保存信号,只是发送者在两种情况下会有所不同...

这是我的模型:

   class Ledger1(models.Model):
        creation_Date = models.DateField()
        name = models.CharField(max_length=32)
        group1_Name = models.ForeignKey(Group1,on_delete=models.CASCADE,blank=True,null=True)
        Opening_Balance = models.DecimalField(max_digits=19,decimal_places=2,blank=True)
        Closing_balance = models.DecimalField(max_digits=10,decimal_places=2,blank=True,null=True)

   class Journal(models.Model):
        Date = models.DateField()
        By = models.ForeignKey(Ledger1,on_delete=models.CASCADE,related_name='Debitledgers')
        To = models.ForeignKey(Ledger1,on_delete=models.CASCADE,related_name='Creditledgers')
        Debit = models.DecimalField(max_digits=10,decimal_places=2)
        Credit = models.DecimalField(max_digits=10,decimal_places=2)

这是我之前做过的:

@receiver(pre_save, sender=Ledger1)
def update_user_closing_balance(sender,instance,*args,**kwargs):
    debit = instance.Debitledgers.aggregate(debit=Sum('Debit'))['debit']
    credit = instance.Creditledgers.aggregate(credit=Sum('Credit'))['credit']
    instance.Closing_balance = instance.Opening_Balance + debit - credit

我想做一个类似的预保存信号,但发件人将是日记...我可以在 django 中使用两个信号来实现相同的功能吗?

我的意思是说,当分类帐更新时,期末余额将自动更新,我已经完成了...

但我也想做一些事情,比如当日记帐更新时,期末余额也会更新...

有人知道吗?

这将触发相关 Ledger1 模型的 pre_save

from django.db.models.signals import post_save
@receiver(post_save, sender=Journal)
def trigger_pre_save(sender, instance, *args, **kwargs):
        instance.By.save()  # trigger pre_save for By Ledger
        # or
        # post_save.send(Ledger1, instance=instance.By, created=False)
        instance.To.save() # trigger pre_save for To Ledger
        # or
        # post_save.send(Ledger1, instance=instance.To, created=False)