Django Admin Dropdown Filtering: Show Exam Class Related Students in Student Assign Field

84 Views Asked by At

I'm working on a school management system using Django, and I'm currently facing an issue with customizing the behavior of dropdown fields in the Django admin interface.

In my application, I have models for managing exams, students, and student results. Each exam is assigned to a subject, which is associated with a specific class and section. When I'm creating a student result entry in the admin panel, I want to ensure that the student_assign field only displays students who are part of the same class as the exam subject.

More specifically, when I select an ExamAssign in the admin form, I want the related student_assign dropdown field to dynamically filter and display only those students who are associated with the same class as the selected exam subject.

enter image description here

If I Select Class 10 here

enter image description here

I want only to show the related class students (not only the section) here.

I've tried implementing this using a custom admin form and overriding the __init__ method, but I'm encountering issues with getting the related students based on the selected exam's subject.

Could anyone guide me through the correct way to achieve this functionality? I'd greatly appreciate any help or advice on how to properly filter the student_assign dropdown to show only students from the same class as the selected exam's subject.

Thank you in advance for your assistance!

1

There are 1 best solutions below

1
Sultan Abdulmanea On

I apologize to you, I didn't understand perfectly, but I think the solution lies in using relationships such as many to many.

Django many_to_many