Quick fix for subject selection in student status admin

This commit is contained in:
Keannu Bernasol 2023-09-17 19:40:27 +08:00
parent 7672f487c8
commit 532460e1ba

View file

@ -2,7 +2,7 @@ from django.contrib import admin
from .models import StudentStatus from .models import StudentStatus
from leaflet.admin import LeafletGeoAdmin from leaflet.admin import LeafletGeoAdmin
from django import forms from django import forms
from subjects.models import SubjectInstance from subjects.models import SubjectInstance, Subject
from accounts.models import CustomUser from accounts.models import CustomUser
@ -14,12 +14,18 @@ class CustomStudentStatusForm(forms.ModelForm):
user=self.instance.user).first() user=self.instance.user).first()
user = CustomUser.objects.filter( user = CustomUser.objects.filter(
id=studentstatus.user.id).first() id=studentstatus.user.id).first()
subjects = SubjectInstance.objects.filter( subject_instances = SubjectInstance.objects.filter(
course=user.course) course=user.course)
# Get the names of the SubjectInstance objects
subject_instance_names = subject_instances.values_list(
'subject', flat=True)
# Filter the Subject objects by these names
subjects = Subject.objects.filter(name__in=subject_instance_names)
self.fields['subject'].queryset = subjects self.fields['subject'].queryset = subjects
subject = forms.ModelMultipleChoiceField( subject = forms.ModelChoiceField(
queryset=SubjectInstance.objects.none(), required=False) queryset=Subject.objects.none(), required=False)
class Meta: class Meta:
model = StudentStatus model = StudentStatus