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))