You have to add a year filter in the results query line.
results=Grade.objects.filter(person=obj).aggregate(Avg("mark"))
On Mon, Aug 1, 2022, 3:09 PM kateregga julius <julikats2018@gmail.com> wrote:
Hello Friends, i have the following models=========================================class Person(models.Model):
fname=models.CharField(verbose_name="First Name",max_length=20,blank=False)
lname = models.CharField(verbose_name="Last Name", max_length=20, blank=False)
class Meta:
ordering =('id','fname','lname')
verbose_name="Person"
verbose_name_plural="Person"
def __str__(self):
return self.fname +" "+self.lname
class Year (models.Model):
year = models.PositiveIntegerField(verbose_name="Year",unique=True )
class Meta:
verbose_name = "Year"
verbose_name_plural = "Year"
def __str__(self):
return str(self.year)
class Courses (models.Model):
course = models.CharField(verbose_name="Course", max_length=20, blank=False,unique=True)
year = models.ForeignKey(Year,verbose_name="Year",on_delete=models.SET_NULL,null=True )
class Meta:
ordering = ('id', 'course','year')
verbose_name = "Coures"
verbose_name_plural = "Coures"
def __str__(self):
return str(self.course)
class PersonCourse(models.Model):
person = models.ForeignKey(Person, verbose_name="Person", on_delete=models.SET_NULL,null=True)
course = models.ForeignKey(Courses,verbose_name="Course",on_delete=models.SET_NULL,null=True)
year = models.ForeignKey(Year, verbose_name="Year", on_delete=models.SET_NULL, null=True)
class Meta:
ordering = ('id', 'person','course')
verbose_name = "PersonCourse"
verbose_name_plural = "PersonCourse"
def __str__(self):
return self.person.lname +" "+self.person.lname
class Grade(models.Model):
person = models.ForeignKey(Person, verbose_name="Person", on_delete=models.SET_NULL,null=True)
course = models.ForeignKey(Courses,verbose_name="Course",on_delete=models.SET_NULL,null=True)
year = models.ForeignKey(Year, verbose_name="Year", on_delete=models.SET_NULL, null=True)
mark=models.PositiveIntegerField(verbose_name="Marks" )
class Meta:
ordering = ('id', 'person','course')
verbose_name = "Grade"
verbose_name_plural = "Grade"
def __str__(self):
return self.person.lname +" "+self.person.lname +" "+self.course.course===================== function ==========def Average(self,obj):
results=Grade.objects.filter(person=obj).aggregate(Avg("mark"))
return results['mark__avg']
Average.short_description="Average"====================================if i run the function it returns the average of all the subjects in the different year.However i want it only to get the overage for a student in a separate year--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9b9fb098-e560-4ace-8157-596a83d601b6n%40googlegroups.com.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABJxPrGTH5DHHMw4oKVRizTr73%2BOgZ6n1%3DCkxbwDbYHuWLc8PQ%40mail.gmail.com.
No comments:
Post a Comment