title: How to perform conditional aggregation on a Django model's one to many related field?
body: <p>Given the following model schemas:</p>
  <pre><code>class Transaction(models.Model):
    wallet = models.ForeignKey(related_name=&quot;transactions&quot;)
    amount = models.DecimalField()  # must be positive value
    type = models.CharField(choices=[(&quot;deposit&quot;, &quot;deposit&quot;), (&quot;withdrawal&quot;, &quot;withdrawal&quot;)]
  
  class Wallet(models.Model):
    pass
  </code></pre>
  <p>What is the most efficient Django ORM query to obtain the balances of every wallet record in the database?</p>
  <p><code>current_balance = sum of &quot;amounts&quot;, where type=&quot;deposits&quot; - sum of &quot;amounts&quot;, where type=&quot;withdrawal&quot;</code></p>
  <p>Let's assume we cannot change the columns or fields of the given tables / records and <code>amount</code> must remain a positive value.</p>
  <p>Thoughts?</p>