如何从views.py Django将数据存储在数据库中?

时间:2018-12-07 07:01:42

标签: django django-models django-views

这是我的模特

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 

在我看来,我已经做到了:

{{1}}

我想将'closing_balance'的值存储到名为'Closing_Balance'的模型字段中。...并在进行任何更改时自动更新它...

任何人都知道在django中这是怎么可能的吗?

谢谢

1 个答案:

答案 0 :(得分:1)

您可以尝试:

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 
ledger1_detail = ledger1.objects.get(pk=ledger1_details.pk)
ledger1_detail.Closing_balance = closing_balance
ledger1_detail.save(update_fields=['Closing_balance'])