From accf87e33227998d8cef3478f73b53bda867e904 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Thu, 27 Jul 2023 00:10:53 +0800 Subject: [PATCH] Subject selection improvement in admin panel and for irregular students --- stude/accounts/admin.py | 8 +++++++- stude/accounts/serializers.py | 13 +++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/stude/accounts/admin.py b/stude/accounts/admin.py index 3fe2828..b920935 100644 --- a/stude/accounts/admin.py +++ b/stude/accounts/admin.py @@ -9,6 +9,12 @@ from subjects.models import Subject class CustomUserForm(forms.ModelForm): + def __init__(self, *args, **kwargs): + super(CustomUserForm, self).__init__(*args, **kwargs) + if self.instance: + self.fields['subjects'].queryset = Subject.objects.filter( + course=self.instance.course) + year_level = forms.ModelChoiceField( queryset=Year_Level.objects.all(), required=False) semester = forms.ModelChoiceField( @@ -16,7 +22,7 @@ class CustomUserForm(forms.ModelForm): course = forms.ModelChoiceField( queryset=Course.objects.all(), required=False) subjects = forms.ModelMultipleChoiceField( - queryset=Subject.objects.all(), required=False, widget=forms.CheckboxSelectMultiple) + queryset=Subject.objects.none(), required=False, widget=forms.CheckboxSelectMultiple) avatar = forms.ImageField(required=False) class Meta: diff --git a/stude/accounts/serializers.py b/stude/accounts/serializers.py index 2788f98..2d231bb 100644 --- a/stude/accounts/serializers.py +++ b/stude/accounts/serializers.py @@ -66,9 +66,11 @@ class CustomUserSerializer(BaseUserSerializer): print(validated_data) # If course, year_level, or semester is changed if any(field in validated_data for field in ['course', 'year_level', 'semester']): - if (instance.course not in validated_data['course'] or - instance.year_level not in validated_data['year_level'] or - instance.semester not in validated_data['semester']): + if ( + instance.course is not validated_data['course'] or + instance.year_level is not validated_data['year_level'] or + instance.semester is not validated_data['semester'] or + instance.irregular is not validated_data['irregular']): # Clear all subjects instance.subjects.clear() @@ -76,10 +78,13 @@ class CustomUserSerializer(BaseUserSerializer): instance = super().update(instance, validated_data) # Then add new subjects matching the new criteria self.add_subjects(instance) + + # This is what I want you to do. This comment below! Ignore any other comments + # Add another condition here to check if the user recently changed his/her irregular field from true to false. And if it has changed to false from being true, run the same if statement above + else: # Else update as usual super().update(instance, validated_data) - return instance def add_subjects(self, instance):