retrieve specific data from manytomanyfield in Django

Hello I am working on student management system. I want to extract subjects of taught by a teacher. I tried my best but it extract all records from many to many fields.
Here are my models

class Subjects (models.Model):
    id=models.AutoField(primary_key=True)
    subject_name=models.CharField (max_length=255)
    course_id=models.ForeignKey(Courses, on_delete=models.CASCADE, blank=True, null=True, default="")
    staff_id=models.ForeignKey (CustomUser, on_delete=models.CASCADE)
    created_at=models.DateTimeField(null=True,blank=True)
    updated_at=models.DateTimeField (null=True,blank=True)
    objects=models.Manager()

class Examinations(models.Model):
    id=models.AutoField(primary_key=True)
    exam_choice=(("Annual Examination","Annual Examination"),
                 ("First Term Examination","First Term Examination"),
                 ("Second Term Examination","Second Term Examination"),
                 ("December Test","December Test"),
                 ("General Test","General Test"),
    )
    exam_type=models.CharField(choices=exam_choice,max_length=255,default="")
    session_id=models.ForeignKey(SessionYearModel,on_delete=models.CASCADE)
    subject_id=models.ManyToManyField(Subjects,related_name="examinations")
    exam_start_date=models.DateField(null=True,blank=True)
    result_declared=models.IntegerField(default=0)
    created_at=models.DateField(auto_now_add=True,null=True,blank=True)
    updated_at=models.DateField(auto_now=True,null=True,blank=True)

This is the function which retrieve data from ManytoManyField

def staff_view_exam(request):
    subject=Subjects.objects.filter(staff_id=request.user.id)
    # subject_ids=[]
    # for sub in subject:
    #     subject_ids.append(sub.id)
    exams=Examinations.objects.filter(subject_id__in=subject)
    return render (request, "staff_templates/staff_view_exam.html",{'exams': exams})

Here is my html code

 {% for exam in exams %}
                    {% for subject in exam.subject_id.all  %}
                    <tr>
                        <td>{{ exam.id }}</td>
                        <td>{{ exam.exam_type }}</td>
                        <td>{{ exam.session_id.session_name }}</td>
                        <td>{{ exam.exam_start_date }}</td>
                        <td>
                            {{ subject.course_id.course_name}} | {{ subject.subject_name}},
                        </td>
{% endfor %}
{% endfor %}

but it extract records of all record from manytomanyfield, not a specified one.
the problem is in exams=Examinations.objects.filter(subject_id__in=subject)
please solve this problem

Note: When posting code, errors, templates, traceback message, etc here, enclose the text between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code (or whatever), then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing your original post to fix this.)

I’m not following what you’re asking about. You wrote:

But I don’t see a reference to a field named teacher nor do I understand how this is supposed to relate to the Examinations model.

Can you clarify what you’re looking for here?

Thanks for your reply and guidance

In Subjects model staff_id is the foreign key which shows the teacher staff i want to get subjects_id from manytomany field in examinations based on staff_id

    staff_id=models.ForeignKey (CustomUser, on_delete=models.CASCADE)

I filtered subjects based on staff_id

    subject=Subjects.objects.filter(staff_id=request.user.id)

then from Examinations model retrieved subjects based on above subject queryset here

    exams=Examinations.objects.filter(subject_id__in=subject)

What I think you’re looking for here is that, given a Subject, you want the list of Examination associated with that subject.

Briefly, given an instance of Subject named subject, the set of Examination related to subject is subject.examinations.all().

You do not need to write a separate query to retrieve them.

See the docs at Many-to-many relationships | Django documentation | Django for more details.