I'm a beginner working on a school fees management software with Django.
My Problem is Below:
I have two models, Student and Family:
from django.db import models
--snip--
class Family(models.Model):
father = models.CharField(max_length=100)
mother = models.CharField(max_length=100)
total_fees = students.fees.aggregate(models.Sum('fees'))
def __str__(self):
return self.father+" " +self.mother
class Student(models.Model):
family = models.ForeignKey(Family, on_delete=models.CASCADE, related_name='students', default='')
name = models.CharField(max_length=100)
date_of_birth = models.DateField('Date Of Birth')
fees = models.IntegerField(default=0)
email= models.CharField(max_length=200)
def __str__(self):
return self.name
Im trying to get all the total fees of a family by adding up the fees of all the students in the family:
total_fees = students.fees.aggregate(models.Sum('fees'))
I used the solution stated here.
but i get this error when i try to migrate:
File "/home/my_username/mysite/students_app/models.py", line 34, in Family
total_fees = students.fees.aggregate(models.Sum('fees'))
AttributeError: 'ForeignKey' object has no attribute 'fees'
What is the correct way to get the total fees for a family?
Thanks!
PS: This is my first question, please tell me how I can improve my questions :)