This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 10th, 2012  |  syntax: None  |  size: 0.95 KB  |  views: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from django.db.models.signals import post_save
  2. from signals import do_something
  3.  
  4. class TransactionAdmin(models.Model):
  5.     ...
  6.  
  7. post_save.connect(do_something, sender=TransactionAdmin)
  8.        
  9. def do_something(sender, instance, created, *args, **kwargs):
  10.     if created and sender == TransactionAdmin:
  11.         ...
  12.     ...
  13.        
  14. def save_related(self,request,form,formsets,change):
  15.             form.save_m2m()
  16.  
  17.             debtors = form.instance.debtors.all()
  18.             n_debtors = debtors.count()
  19.             creditor = form.instance.creditor
  20.             value = form.instance.value
  21.  
  22.             if form.instance.split:
  23.                     value = value/(n_debtors + 1)
  24.  
  25.             for debtor in debtors:
  26.                     p = Person.objects.get(id = debtor.id)
  27.                     p.balance = p.balance - value
  28.                     p.save()
  29.  
  30.             p = Person.objects.get(id = creditor.id)    
  31.             p.balance = p.balance + n_debtors*value
  32.             p.save()
clone this paste RAW Paste Data