I have two Django models - Teacher and Student and have a many-to-many relationship. Teachers can have multiple students and students can have multiple teachers. There is 'through' model called 'Remarks' where a teacher can mark a student as favourite.
I am new to GraphQL. I am trying to implement two queries:
1. Teachers and all of their students
2. Teachers and their favourite students
I am having difficulty in implementing the second query and have been unable to do so.
models.py
SUBJECTS = (
("Maths", "Maths"),
("Chemistry", "Chemistry"),
("Physics", "Physics")
)
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField(default=0)
def __str__(self):
return self.name
class Teacher(models.Model):
name = models.CharField(max_length=100)
subject = models.CharField(max_length=100, choices=SUBJECTS)
students = models.ManyToManyField(Student, through="Remarks")
def __str__(self):
return self.name
class Remarks(models.Model):
student = models.ForeignKey(Student, related_name="student", on_delete=models.DO_NOTHING)
teacher = models.ForeignKey(Teacher, related_name="teacher", on_delete=models.DO_NOTHING)
favorite = models.BooleanField(default=False, choices=(
(True, "Yes"),
(False, "No")
))
schema.py
import graphene
from graphene import relay, ObjectType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import Teacher, Student, Remarks
class RemarkNode(DjangoObjectType):
class Meta:
model = Remarks
filter_fields = ['favorite']
interfaces = (relay.Node, )
class TeacherNode(DjangoObjectType):
favorite = graphene.Field(RemarkNode)
class Meta:
model = Teacher
filter_fields = ['name', 'subject']
interfaces = (relay.Node, )
class StudentNode(DjangoObjectType):
class Meta:
model = Student
filter_fields = {
'name': ['exact', 'icontains', 'istartswith'],
'age': ['exact'],
'favorite': ['exact']
}
interfaces = (relay.Node, )
class Query(graphene.ObjectType):
teacher = relay.Node.Field(TeacherNode)
all_teachers = DjangoFilterConnectionField(TeacherNode)
student = relay.Node.Field(StudentNode)
all_students = DjangoFilterConnectionField(StudentNode)
def resolve_favorites(self, info, **kwargs):
teacher = Teacher.objects.get(pk=self.id)
remarks = Remarks.objects.filter(teacher=teacher)
return [each.student for each in remarks]