Puteri
Puteri

Reputation: 71

Store sum from views to database django

I am trying to store the total attendance of the student in percentage to the database without form.

the Views.py

 def attStudName(request):
#to display each student name with their total mark in table form
students = MarkAtt.objects.values('studName__VMSAcc').annotate(mark=Sum('attendance'))

    #to convert the total mark to percentage and save in FinalAtt table
    mark = 0
mark += students.attendance
work = FinalAtt.objects.all()
for stud in students:
    stud.mark = (stud.mark/1100) * 100
    work.VMSAcc = students
    work.finalMark = mark
    work.save()
context = {
'students' : students


    }
return render(request,'show-name.html',context)

MarkAtt Model:

class MarkAtt(models.Model):

studName = models.ForeignKey(Namelist,on_delete=models.SET_NULL,blank=True, null=True, default=None)
classGrp = models.ForeignKey(GroupInfo, on_delete=models.SET_NULL, null=True)
currentDate = models.DateField(default=now())
week = models.IntegerField(default=1)
attendance = models.IntegerField(default=100) 

FinalAtt Model:

  class FinalAtt(models.Model):
VMSAcc= models.ForeignKey(Namelist, on_delete=models.SET_NULL, null=True)
finalMark = models.DecimalField(max_digits=5, decimal_places=2)

The error i am getting is:

'QuerySet' object has no attribute 'attendance'

How do i resolve this and save the information i want successfully?

Upvotes: 0

Views: 106

Answers (1)

Vincent Yeadon
Vincent Yeadon

Reputation: 98

students is a QuerySet, so you can't do mark += students.attendance.

You most likely want to loop through them to calculate mark.

Upvotes: 1

Related Questions