Queries to multiple tables

42 views
Skip to first unread message

Kristofer

unread,
Nov 10, 2012, 2:23:44 AM11/10/12
to django...@googlegroups.com
Hello,

I'm new to Django and trying to learn it by making a simple budget/checkbook application.

I apologize for my ignorance, but I couldn't seem to find an answer to this on Google or the Django docs from my searches.

I want to create a function that will take a bank account as an id number (assumed to already exist), and the payee/amount.  From there, the function will do a typical bank transaction: 1) add the entry to the "Ledger" table, and subtract the amount from the "balance" field of the "Account" table for the account referenced in the id number passed to the function.

Where is the appropriate/intended place to put that type of function?  Model, Manager, View, or somewhere completely different?

My best guess is that it belongs in a Manager, but all of the examples I can find seem to indicate that Managers are intended for queries.

Thanks in advance,

Kris



Tomas Ehrlich

unread,
Nov 10, 2012, 6:15:36 AM11/10/12
to django...@googlegroups.com
Hi Kristofer,
I usualy put methods like this one into Model, although Manager is also
possible. Definitely not View or sth else.

Why I like Model more than Manager is this difference (suppose your
method is called make_payment):

Account.make_payment # via Model
Account.objects.make_payment # via Manager

and because this method is "creating" object instead of "retrieving"
objects, it seems to me more apropriate. I know, there are methods like
create or get_or_create in Manager, so propably someone would have a
better arguments on this. Or maybe it's just matter of taste. In fact,
in one application of mine I have method Node.create which do some
fancy stuff (creating other related objects, consistency checks, etc.)
and then call Node.objects.create.


Cheers,
Tom

Dne Sat, 10 Nov 2012 01:23:44 -0600 (CST)
Kristofer <kris...@cybernetik.net> napsal(a):

Javier Guerra Giraldez

unread,
Nov 10, 2012, 12:22:32 PM11/10/12
to django...@googlegroups.com
On Sat, Nov 10, 2012 at 6:15 AM, Tomas Ehrlich <tomas....@gmail.com> wrote:
> I usualy put methods like this one into Model, although Manager is also
> possible. Definitely not View or sth else.

definitely in the model.

you'd use it like this:

accnt = get_object_or_404 (Account, pk=account_id)
payee = get_object_or_404 (...........)
accnt.make_payment (payee, amount)

it would be as easy as adding a method to your Account class, for example:

class Account (models.Model):
......
......

def make_payment(self, payee, amount, save=True):
if amount > self.balance:
throw NotEnoughFunds
self.balance -= amount
payee.balance += amount
if save:
self.save()
payee.save()

(of course, a serious accounting app don't modify balances but
registers debit/credit entries)

--
Javier

Matt Schinckel

unread,
Nov 11, 2012, 6:02:23 PM11/11/12
to django...@googlegroups.com
Following up: if it affects a single instance of an object, then it belongs as a model method.

If it affects any number of them, then it should perhaps go on the manager 
(or even the queryset: I use django-model-utils PassThroughManager for this a lot).

Matt.
Reply all
Reply to author
Forward
0 new messages