Graphene-Django and many to many relationship lookup
Asked Answered
G

1

15

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]
Grewitz answered 11/10, 2019 at 14:18 Comment(0)
C
0

schema.py

class TeacherNode(DjangoObjectType):
favorite = graphene.List(RemarkNode)

def resolve_favorite(self, info, **kwargs):
    remarks = Remarks.objects.filter(teacher=self.id)
    return remarks

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'],
        }
        interfaces = (relay.Node, )

class TeacherQuery(graphene.ObjectType):
    teacher = relay.Node.Field(TeacherNode)
    all_teachers = DjangoFilterConnectionField(TeacherNode)

    student = relay.Node.Field(StudentNode)
    all_students = DjangoFilterConnectionField(StudentNode)
Caboose answered 8/4, 2022 at 15:11 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Lyrate

© 2022 - 2024 — McMap. All rights reserved.