From 0f1fddb6f5f9eedf7c5b2ccfb84952a552ccaa68 Mon Sep 17 00:00:00 2001 From: Danil Andreev Date: Sun, 5 Feb 2023 19:36:35 +0300 Subject: [PATCH] SPORT-636: Handle 'relation semester does not exist' --- adminpage/sport/models/student.py | 45 ++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/adminpage/sport/models/student.py b/adminpage/sport/models/student.py index dc82a0c3..fb0de36c 100644 --- a/adminpage/sport/models/student.py +++ b/adminpage/sport/models/student.py @@ -22,33 +22,36 @@ def validate_course(course): class StudentManager(models.Manager): - - def get_queryset(self): from api.crud import get_ongoing_semester from api.crud import SumSubquery from sport.models import Attendance, Debt + from django.db.utils import ProgrammingError qs = super().get_queryset() - qs = qs.annotate(_debt=Coalesce( - SumSubquery(Debt.objects.filter( - semester_id=get_ongoing_semester().pk, - student_id=OuterRef("pk")), - 'debt', - ), - 0 - )) - qs = qs.annotate(_ongoing_semester_hours=Coalesce( - SumSubquery(Attendance.objects.filter( - training__group__semester_id=get_ongoing_semester().pk, - student_id=OuterRef("pk")), - 'hours', - ), - 0 - )) - qs = qs.annotate(hours=ExpressionWrapper( - F('_ongoing_semester_hours') - F('_debt'), output_field=IntegerField() - )) + + try: + qs = qs.annotate(_debt=Coalesce( + SumSubquery(Debt.objects.filter( + semester_id=get_ongoing_semester().pk, + student_id=OuterRef("pk")), + 'debt', + ), + 0 + )) + qs = qs.annotate(_ongoing_semester_hours=Coalesce( + SumSubquery(Attendance.objects.filter( + training__group__semester_id=get_ongoing_semester().pk, + student_id=OuterRef("pk")), + 'hours', + ), + 0 + )) + qs = qs.annotate(hours=ExpressionWrapper( + F('_ongoing_semester_hours') - F('_debt'), output_field=IntegerField() + )) + except ProgrammingError: + pass return qs