Given the following model schemas:
class Transaction(models.Model):
wallet = models.ForeignKey(related_name="transactions")
amount = models.DecimalField() # must be positive value
type = models.CharField(choices=[("deposit", "deposit"), ("withdrawal", "withdrawal")]
class Wallet(models.Model):
pass
What is the most efficient Django ORM query to obtain the balances of every wallet record in the database?
current_balance = sum of "amounts", where type="deposits" - sum of "amounts", where type="withdrawal"
Let's assume we cannot change the columns or fields of the given tables / records and amount
must remain a positive value.
Thoughts?