在复式会计中确保交易始终保持平衡的最佳方法是什么?
我正在 Django 中创建一个复式记账应用程序。我有这些模型:
class Account(models.Model):
TYPE_CHOICES = (
('asset', 'Asset'),
('liability', 'Liability'),
('equity', 'Equity'),
('revenue', 'Revenue'),
('expense', 'Expense'),
)
num = models.IntegerField()
type = models.CharField(max_length=20, choices=TYPE_CHOICES, blank=False)
description = models.CharField(max_length=1000)
class Transaction(models.Model):
date = models.DateField()
description = models.CharField(max_length=1000)
notes = models.CharField(max_length=1000, blank=True)
class Entry(models.Model):
TYPE_CHOICES = (
('debit', 'Debit'),
('credit', 'Credit'),
)
transaction = models.ForeignKey(Transaction, related_name='entries')
type = models.CharField(max_length=10, choices=TYPE_CHOICES, blank=False)
account = models.ForeignKey(Account, related_name='entries')
amount = models.DecimalField(max_digits=11, decimal_places=2)
Run Code Online (Sandbox Code Playgroud)
我想在模型级别强制执行平衡事务,但似乎在正确的位置没有挂钩。例如,Transaction.clean 将不起作用,因为首先保存事务,然后由于 Entry.transaction ForeignKey 添加条目。
我也希望在 admin 中进行平衡检查。目前,我使用带有干净方法的 EntryInlineFormSet 来检查管理员中的余额,但这在从脚本添加事务时无济于事。我愿意更改我的模型以使其更容易。