I have a simple model that collects data (specifically integers) to display an election results in a table. I have 2 regions (more of course but let's say two for now) where data will be coming from. I want to display each regions and the total for each region and then display the SUM at the bottom of the table
My base model is as follows;
class Results(models.Model)
region_number = models.Foreignkey(Regions)
region_name = models.Charfield()
polling_station = models.Charfield()
party1 = models.IntegerField()
party2 = models.IntegerField()
party3 = models.IntegerField()
Then I have two class inheritance
class GeneralElections(Results)
pass
class RegionalElections(Results)
pass
And I want the printed output in my template to look something like this
---------------------------------
regions party1 party2 party3
---------------------------------
1 20 40 60
2 20 80 100
---------------------------------
Total 80 120 160 <------- More Importantly This!
---------------------------------
Additionally I want to be able to have summary page that shows
General Elections
-----------------------
party1 party2 party3
-----------------------
80 120 160
Regional Elections
-----------------------
party1 party2 party3
-----------------------
50 85 210
Up to this point I have been able to accomplish everything, except being able to print the SUM of each column at the bottom of the table
Everywhere I've looked it said to use django aggregation. However the instructions are a bit vague. Maybe because I am a beginner. I am confused where to put the respective codes for example.
from django.db.import SUM
- Where does this import go? In models.py? or views.py?
total = Tally.objects.aggregate(Sum("field_name"))
Where do I put this function?
Which field_name do I use? The are multiple field names I want SUM for. One for each party
I apologize if this has been answered before but I am strapped for time and I couldn't find any detailed information regarding this.
I greatly appreciate if someone can point me in the right direction and save me a lot of time as I need to get this app online in a matter of hours.
Thank you in advance!