I'm working on a simple budgeting app. I would like my account balance to be affected any time I add a new transaction.
views.py:
class TransactionList(ListView):
template_name = 'envelope/transaction_list.html'
model = Transaction
def get_context_data(self, **kwargs):
context = super(TransactionList, self).get_context_data(**kwargs)
context['account_list'] = Account.objects.all()
return context
class TransactionCreate(CreateView):
template_name = 'envelope/transaction_create.html'
model = Transaction
fields = '__all__'
success_url = reverse_lazy('transaction_list')
models.py
class Transaction(models.Model):
date = models.DateField(default=datetime.today)
amt = models.DecimalField(decimal_places=2, max_digits=8)
envelope = models.ForeignKey('Envelope')
desc = models.CharField(max_length=50)
def __unicode__(self):
return u'%s - %s - %s' % (self.date, self.amt, self.desc)
class Account(models.Model):
name_first = models.CharField(max_length=50)
name_last = models.CharField(max_length=50)
amt = models.DecimalField(decimal_places=2, max_digits=8)
How do I change the Account.amt field when a transaction is created?
envelopein theTransactionmodel? how to know the account from a transaction?