filter many to many field from a list

Here are my models

class Subjects (models.Model):
    subject_name=models.CharField ()

class Examinations(models.Model):
    exam_choice=(("Annual ","Annual"),
                 ("First Term","First Term"),
                 ("Second Term","Second Term"),
                 ("December Test","December Test"),
                 ("General Test","General Test"),

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


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 %}
                        <td>{{ }}</td>
                        <td>{{ exam.exam_type }}</td>
                            {{ subject.course_id.course_name}} | {{ subject.subject_name}},
                        <td><a href="{% url 'edit_exam' %}" class="btn btn-success">Edit</a></td>
                        {% 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))

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


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:


And the result is:

{{ 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 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:


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

# your_template.html

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

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.