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