I am developing a small website with Django, and I have to calculate statistics with data obtained from several tables within the database.
For instance (nothing related to my actual models), for any given user, let us say I would like all kids birthday parties he's dealt with, the ones he spoken with in stated parties. With this, I'd require a wide query, accesing several tables.
Now, in the object-oriented perspective, it is always good when the User class implemented a technique that came back that information. From the database model perspective, I do not like whatsoever the thought of adding functionality to some "row instance" that must query other tables. I must keep all qualities and techniques within the Model classes highly relevant to that single row, in order to avoid scattering the company logic everywhere.
How must i start applying database-wide queries that, from an item-oriented perspective, fit in with just one object? Must I come with an exterior kinda God-object that understands how to collect and organize these details? Or it is possible to better, more elegant solution?
I suggest stretching Django's Model-Template-View approach having a controller. It's my job to possess a controller.py inside my applications the only interface towards the data sources. So inside your above situation I'd have something similar to
This is particularly helpful whenever your "data obtained from several tables within the database" becomes "data obtained from several tables in a number of databases" as well as "data obtained from various sources, e.g. databases, cache backends, exterior apis, etc.".
User.get_attended_birthday_parties() or Event.get_attended_parties(user) work fine: this is an interface which makes sense if you use it. Creating one more "all-purpose" object won't build your code cleaner or simpler to keep.