Как суммировать элементы queryset в forloop в шаблоне django
У меня есть проблема внутри шаблона django. В основном, то, что я хотел бы достичь, это такого рода таблица:
Под каждым столбцом, где есть Absent или present, я хотел бы суммировать продолжительность. Общая продолжительность находится в самом правом столбце, но я хотел бы отфильтровать, по каждому студенту, каждую продолжительность. Чтобы лучше объяснить, студент A и студент D, в этом примере, должны были бы напечатать 5:00.
Я размещаю некоторый код здесь:
models.py
class Attendance(models.Model):
course = models.ForeignKey(
TheoryCourse, on_delete=models.DO_NOTHING, null=True, blank=True)
instructor = models.ForeignKey(
Instructor, on_delete=models.DO_NOTHING, null=True, blank=True)
subject = models.ForeignKey(
Subject, on_delete=models.DO_NOTHING, null=True, blank=True)
date = models.DateField(blank=True, null=True)
start_time = models.TimeField(blank=True, null=True)
end_time = models.TimeField(blank=True, null=True)
learning_objectives = models.TextField(null=True, blank=True)
duration = models.TimeField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class AttendanceReport(models.Model):
attendance = models.ForeignKey(Attendance, on_delete=models.CASCADE)
presence = models.CharField(max_length=100, null=True, blank=True)
student = models.ForeignKey(Student, on_delete=models.DO_NOTHING)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Student(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
date_of_birth = models.DateField()
fiscal_code = models.CharField(max_length=50)
phone = models.CharField(max_length=50)
license = models.ForeignKey(
License, on_delete=models.PROTECT, blank=True, null=True)
picture = models.ImageField(
blank=True, null=True, default='default.png')
id_card = models.ForeignKey(
IDCard, on_delete=models.PROTECT, blank=True, null=True)
address = models.CharField(max_length=100)
cap = models.CharField(max_length=10)
city = models.CharField(max_length=100)
province = models.CharField(max_length=100)
country = models.CharField(max_length=100)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
is_active = models.BooleanField(default=True)
views.py
def course_details(request, pk):
''' Students' Detail and classroom details '''
course = TheoryCourse.objects.get(id=pk)
students = course.student.all()
syllabus = course.theory_syllabus
subjects = syllabus.subject_duration.all()
total_duration = subjects.aggregate(eet=Sum(ExpressionWrapper(
F('duration'), output_field=IntegerField()), output_field=DurationField()))['eet']
if total_duration != None:
total_duration = duration(total_duration)
''' Attendance Reports part '''
attendances = Attendance.objects.filter(course_id=course).order_by('-date')
attendance_reports = AttendanceReport.objects.filter(
attendance_id__in=attendances)
attendance_presence = AttendanceReport.objects.filter(
attendance_id__in=attendances, student_id__in=students, presence='present')
total_classroom_duration = attendances.aggregate(eet=Sum(ExpressionWrapper(
F('duration') * 3600000000, output_field=IntegerField()), output_field=DurationField()))['eet']
total_attendance_presence = attendance_presence.aggregate(eet=Sum(ExpressionWrapper(
F('attendance_id__duration') * 3600000000, output_field=IntegerField()), output_field=DurationField()))['eet']
context = {
'course': course,
'students': students,
'syllabus': syllabus,
'subjects': subjects,
'total_duration': total_duration,
'attendances': attendances,
'attendance_reports': attendance_reports,
'total_classroom_duration': total_classroom_duration,
'total_attendance_presence': total_attendance_presence,
}
return render(request, 'theory/course_details.html', context)
В основном, что я хотел бы понять, это как иметь такой фильтр для каждого студента. total_attendance_presence работает нормально, но для всех студентов. Я понятия не имею, как отобразить в таблице для каждого студента. Любая помощь будет принята с благодарностью.
