filter many to many field from a list

Here are my models

class Subjects (models.Model):
    id=models.AutoField(primary_key=True)
    subject_name=models.CharField ()

class Examinations(models.Model):
    id=models.AutoField(primary_key=True)
    exam_choice=(("Annual ","Annual"),
                 ("First Term","First Term"),
                 ("Second Term","Second Term"),
                 ("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)
    subjects=models.ManyToManyField(Subjects,related_name='examination',)

I want to filter many to many field from a list of subject ids like and i get one row of exam with id 1

    subject_ids=[2,4,5]
    exams=Examinations.objects.filter(subjects__in=subject_ids)

in html template below it display me all subject[1,2,3,4,5,6] related to exam id 1 other than in the list [2,4,5]. I want to display just subject [2,4,5] from many to many fields.

                    {% for exam in exams %}
                    {% for subject in exam.subjects.all %}
                    <tr>
                        <td>{{ exam.id }}</td>
                        <td>{{ exam.exam_type }}</td>
                        <td>
                            {{ subject.course_id.course_name}} | {{ subject.subject_name}},
                             </td>
                        <td><a href="{% url 'edit_exam' exam_id=exam.id %}" class="btn btn-success">Edit</a></td>
                        </tr>
                        {% endfor %} 
                      {% endfor %}

I tried many ways below to do this but in vain. I m stuck here and cannot proceed further

# exams=subject.examination.all()   #examss=Examinations.objects.annotate(count=Count('subjects')).filter(count=len(subject_ids))
    #exams=Examinations.objects.filter(subjects__id__in=subject_ids).annotate(count=Count('subjects')).filter(count=len(subject_ids))

Sorry for the post. I’ve just realized that it is not a solution for the problem. There’s something wrong in the template

exams=Examinations.objects.filter(subjects__in=subject_ids)

I suppose this line was the problem.
I was able to get all the exams with “ids” listed in subject_ids by changing that line to:

exams=Examinations.objects.filter(subjects__id__in=subject_ids)

And the result is:
result

p.s.
{{ subject.course_id.course_name}} was not included into the template because сourse_id was not specified in any of the models.

This query exam.subjects.all will return a queryset of ALL the subjects related to the given exam. And the filtering performed in views.py will have no effect.

sir so what would be the solution
if I don’t use ```
{% for subject in exam.subjects.all %}

It displays single row of exams but not subjects in the list
I tried to filter it in template but it seems that it is not possible

I think you don’t need to try and filter Examinations by given subject_ids.
Because what you really need is a queryset of Subjects and related Examinations.
Maybe a better approach in this case would be to filter Subjects by subject_ids.
Example is given below:

# views.py

def index(request):
    subject_ids = [1, 2]
    subjs = Subjects.objects.filter(id__in=subject_ids).prefetch_related('examinations')
    return render(
        request,
        'your_template.html',
        {'subjs': subjs}
    )

# your_template.html

<table>
{% for subj in subjs %}
  {% for exam in subj.examinations.all %}
  <tr>
    <td>{{ exam.id }}</td>
    <td>{{ exam.exam_type }}</td>
    <td>{{ subj.subject_name }}</td>
    <td><a href="" class="btn btn-success">Edit</a></td>
  </tr>
  {% endfor %}
{% endfor %}
</table>

=====
Note. This is just my opinion on how to make it working. Perhaps more experienced developers will offer you a better solution :wink:

Thanks Sir, now it works.
I am very thankful to you for your assistance.