石墨烯-Django 和多对多关系查找

Graphene-Django and many to many relationship lookup

提问人:dotslash227 提问时间:10/11/2019 更新时间:4/8/2022 访问量:1815

问:

我有两个 Django 模型 - 教师和学生,并且具有多对多关系。教师可以有多个学生,学生也可以有多个教师。有一种称为“备注”的“通过”模型,教师可以将学生标记为最爱。

我是 GraphQL 的新手。我正在尝试实现两个查询:
1.教师和他们所有的学生
2.老师和他们最喜欢的学生

我在实施第二个查询时遇到困难,并且无法这样做。

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]
django python 石墨烯 django-graphql-jwt

评论


答:

0赞 aref 4/8/2022 #1

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)

评论

3赞 Community 4/9/2022
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。