GETTING AVERAGE OF A STUDENT IN DIFFERENT YEARS

48 views
Skip to first unread message

kateregga julius

unread,
Aug 1, 2022, 10:09:53 AMAug 1
to Django users
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
2.jpg
models.jpg

Opeyemi Ogunsanya

unread,
Aug 1, 2022, 1:19:00 PMAug 1
to django...@googlegroups.com
You have to add a year filter in the results query line. 
results=Grade.objects.filter(person=obj).aggregate(Avg("mark"))

--
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...@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.

kateregga julius

unread,
Aug 1, 2022, 3:16:52 PMAug 1
to django...@googlegroups.com

kateregga julius

unread,
Aug 1, 2022, 5:29:57 PMAug 1
to django...@googlegroups.com
If I manually pass in the year_id it works.
My Question is how can I pass the year ID as the third parameter in the method of Average
IMG_20220802_011904_549.jpg

Opeyemi Ogunsanya

unread,
Aug 1, 2022, 10:59:15 PMAug 1
to django...@googlegroups.com
results=Grade.objects.filter(person=obj, year=year-to-select-from).aggregate(Avg("mark"))

kateregga julius

unread,
Aug 2, 2022, 12:17:59 AMAug 2
to django...@googlegroups.com
Hahaha here comes this idiots comment. Have ever seen such a filter?

Opeyemi Ogunsanya

unread,
Aug 2, 2022, 12:48:35 AMAug 2
to django...@googlegroups.com
Screenshot_20220802-054634.jpg
Reply all
Reply to author
Forward
0 new messages