0

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?

3
  • 1
    What is envelope in the Transaction model? how to know the account from a transaction? Commented Jul 10, 2015 at 18:04
  • @RahulGupta Envelope is another model, but irrelevant to this question, so I didn't include it. Commented Jul 10, 2015 at 18:15
  • How to get an account from a transaction? Although, i have added the ans without that information. Commented Jul 10, 2015 at 18:18

2 Answers 2

1
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=Transaction)
def change_account_amt(sender, instance, created, **kwargs):
    if created:
        # instance is a Transaction created object ...
        acc_obj = Account.objects.get(...)
        acc_obj.amt = ...
        acc_obj.save()
Sign up to request clarification or add additional context in comments.

Comments

0

You can do that easily via a post_save signal. It is sent at the end of the save() method.

from django.db.models.signals import post_save


class Transaction(models.Model):
    ....

class Account(models.Model):
    ... 

# method for updating account amount
def update_account_amount(sender, **kwargs):
    instance = kwargs['instance']
    created = kwargs['created']
    raw = kwargs['raw']
    if created and not raw:
        account = Account.objects.get(...) # get the account object
        account.amt -= instance.amt  # update the amount
        account.save() # save the account object

# register the signal
post_save.connect(update_account_amount, sender=Transaction)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.