I am trying to plot some numbers on my charts. But I have a problem summing the number of professionals per district(foreign key). Here is my model.py.
class District(models.Model):
name = models.CharField(max_length=50, blank=True, null=True)
location = models.CharField(max_length=80, blank=True, null=True)
population = models.IntegerField(blank=True, null=True)
class Household(models.Model):
district = models.ForeignKey(District, on_delete=models.CASCADE, null=True)
family_name = models.CharField(max_length=50, blank=True, null=True)
address = models.CharField(max_length=150, blank=True, null=True)
members = models.IntegerField(blank=True, null=True) # no of members
professionals = models.IntegerField(blank=True, null=True) # no of professionals
...
Below is just an overall total of professionals. But how can I sum it by district?
from django.db.models import Sum
def dashboard(request):
stats = Household.objects.annotate(total_prof=Sum('professionals'))