views:

47

answers:

1

I'm working on a site that will help private teachers manage their students, and part of this will be keeping track of how much money the teacher is owed.

I want my apps to be reusable and free from dependency on one another. So, I've created one app whose responsibility is the CRUD of student, teacher, and parent objects (these models all have a foreign key to User, so they're like a user profile, but I'm not using the built in user profile system). There's also a Family model which relates student objects to parent objects. I've also created a separate app whose responsibility is to manage Accounts, Invoices, and Line Items.

My problem lies in the integration of the two. I've got a basic template in the account app powered by a generic view which lists the account objects and their balances. What I want to do in that template is group the accounts by Family and show totals for all the Accounts that belong to a given family.

How does one go about this while keeping the apps from depending on one another?

A: 

Here's what I have so far that seems to be working:

class AccountGroup(models.Model):
    accounts = models.ManyToManyField(Account)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

Documented here. This way accounts can be grouped together by anything they have in common in outside apps. In my case, when I create an account group, I set content_type to family, and object_id to the particular family instance it should be grouped by. The view is a simple wrapped generic view.

jobrahms