title: What's the best way to ensure balanced transactions in a double-entry accounting app?
body: <p>What's the best way to ensure that transactions are always balanced in double-entry accounting?</p>
  
  <p>I'm creating a double-entry accounting app in Django.  I have these models:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I'd like to enforce balanced transactions at the model level but there doesn't seem to be hooks in the right place.  For example, Transaction.clean won't work because transactions get saved first, then entries are added due to the Entry.transaction ForeignKey.</p>
  
  <p>I'd like balance checking to work within admin also.  Currently, I use an EntryInlineFormSet with a clean method that checks balance in admin but this doesn't help when adding transactions from a script.  I'm open to changing my models to make this easier.</p>