Help on ModelForm DropDowns


I’m trying to build a chained dropdown model form and looking forward to keeping the selected dropdown values even if I submit the form for filtering but I’m having a challenge on keeping the foreign key values in the dropdown. Below is the code I’m trying to use. Any help will be greatly appreciated.

    $(document).ready(function () {
        let exam = $('#Form').data('id_exam')
        let classroom = $('#Form').data('id_classroom')
        let subject = $('#Form').data("id_subject");

        if (exam) {
            $('#id_exam option[value=' + exam + ']').attr('selected', 'selected');
        if (classroom) {
            $('#id_classroom option[value=' + classroom + ']').attr('selected', 'selected');
        if (subject) {
            $('#id_subject option[value=' + subject + ']').attr('selected', 'selected');