This is my models:
class ledger1(models.Model):
name = models.CharField(max_length=32)
Closing_balance = models.DecimalField(max_digits=10,decimal_places=2,blank=True,null=True)
class journal(models.Model):
Date = models.DateField(default=datetime.date.today)
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,null=True)
Credit = models.DecimalField(max_digits=10,decimal_places=2,null=True)
And in my views I have done this:
qscb = journal.objects.filter(By=ledger1_details.pk, Date__gte=selectdatefield_details.Start_Date, Date__lte=selectdatefield_details.End_Date)
qscb2 = journal.objects.filter(To=ledger1_details.pk, Date__gte=selectdatefield_details.Start_Date, Date__lte=selectdatefield_details.End_Date)
total_debitcb = qscb.aggregate(the_sum=Coalesce(Sum('Debit'), Value(0)))['the_sum']
total_creditcb = qscb2.aggregate(the_sum=Coalesce(Sum('Credit'), Value(0)))['the_sum']
if(ledger1_details.group1_Name.balance_nature == 'Debit'):
closing_balance = opening_balance + total_debitcb - total_creditcb
else:
closing_balance = opening_balance + total_creditcb - total_debitcb
I want to store the value of 'closing_balance' into my model field named 'Closing_Balance'....And automatically update it when any changes are made...
Any idea anyone how it is possible in django?
Thank you
closing_balanceintoledger1_details?