How to find student course registrations and teacher assignments?

I’ve set up my Django models for a school system. Now I need help figuring out how to get info about student registrations and teacher assignments. Here’s what I’ve got:

from django.db import models

class Instructor(models.Model):
    name = models.CharField(max_length=100)

class Pupil(models.Model):
    name = models.CharField(max_length=100)
    id_number = models.IntegerField()

class Subject(models.Model):
    title = models.CharField(max_length=100)
    code = models.CharField(max_length=20)

class Enrollment(models.Model):
    pupil = models.ForeignKey(Pupil, on_delete=models.CASCADE)
    subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
    instructor = models.ForeignKey(Instructor, on_delete=models.CASCADE)
    date_enrolled = models.DateTimeField(auto_now_add=True)

How can I query these models to find out which pupils are enrolled in which subjects, and which instructors are teaching them? Any help would be great!

To find student course registrations and teacher assignments with your current model setup, you can use Django’s powerful ORM querying capabilities. Here’s how you can approach it:

For student enrollments:

student = Pupil.objects.get(id=student_id)
enrollments = Enrollment.objects.filter(pupil=student)
for enrollment in enrollments:
    print(f'{student.name} is enrolled in {enrollment.subject.title} taught by {enrollment.instructor.name}')

For teacher assignments:

teacher = Instructor.objects.get(id=teacher_id)
assignments = Enrollment.objects.filter(instructor=teacher)
for assignment in assignments:
    print(f'{teacher.name} is teaching {assignment.subject.title} to {assignment.pupil.name}')

These queries will give you the information you need. You can further refine them based on specific requirements, like filtering by date or subject.