From 0007f84b47ae4514e4aaeaba56d1315b8810e040 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Fri, 27 Oct 2023 23:32:41 +0800 Subject: [PATCH] Also delete empty study groups when students move between groups --- stude/student_status/serializers.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/stude/student_status/serializers.py b/stude/student_status/serializers.py index 320b463..6276c15 100644 --- a/stude/student_status/serializers.py +++ b/stude/student_status/serializers.py @@ -67,6 +67,12 @@ class StudentStatusSerializer(serializers.ModelSerializer): # If there are no students left in the old StudyGroup, delete it old_study_group.delete() + # If student has changed study group, check if the old study_group no longer has any students + if study_group is not old_study_group and old_study_group is not None: + if not old_study_group.students.exists(): + # If there are no students left in the old StudyGroup, delete it + old_study_group.delete() + return instance