在复式记账法中确保交易始终保持平衡的最佳方式是什么?
我正在 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)
我想在模型级别强制执行平衡交易,但在正确的地方似乎没有挂钩。例如,Transaction.clean 不起作用,因为首先保存事务,然后由于 Entry.transaction ForeignKey 而添加条目。
我希望余额检查也可以在管理员中工作。目前,我使用 EntryInlineFormSet 和一个干净的方法来检查管理员中的余额,但这在从脚本添加事务时没有帮助。我愿意更改我的模型以使其更容易。