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):
    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)
    updated_at=models.DateTimeField (null=True,blank=True)

class Examinations(models.Model):
    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"),

This is the function which retrieve data from ManytoManyField

def staff_view_exam(request):
    # subject_ids=[]
    # for sub in subject:
    #     subject_ids.append(
    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  %}
                        <td>{{ }}</td>
                        <td>{{ exam.exam_type }}</td>
                        <td>{{ exam.session_id.session_name }}</td>
                        <td>{{ exam.exam_start_date }}</td>
                            {{ subject.course_id.course_name}} | {{ subject.subject_name}},
{% 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


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


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.