Hi there, I'm struggling with the design of a django application. Given the following models:
class A(models.Model):
name = models.CharField(max_length=255)
class B(models.Model):
name = models.CharField(max_length=255)
a = models.ForeignKey(A)
class C(models.Model):
val = models.IntegerField()
b = models.ForeignKey(B)
I would like a view/template that displays a HTML table that shows in the first column all A objects, in the second column all B objects (grouped by A) that are referenced to A and in the last column a sum of all val's of C objects that are referenced to each B. All of that with a sum for each A object. The following example shows what I'm looking for:
A1.name | B1.name [where FK to A1] | sum(C.val) [where FK to B1] A1.name | B2.name [where FK to A1] | sum(C.val) [where FK to B2] A1.name | Total | sum(C.val) [where FK to Bx (all B that have FK to A1] A2.name | B3.name [where FK to A2] | sum(C.val) [where FK to B3] A2.name | Total | sum(C.val) [where FK to Bx (all B that have FK to A2]
Can anyone give me an advice how to design such a problem (unfortunately, my code often ends up in quite a mess)?
Should I extend the model classes with appropriate methods? Do a custom query for the whole table data in the view? Just get all the objects via managers and do most of the things in the template?
Thanks for every answer.
Regards,
Luke.