Django-Graphene - 与同类型对象的关系

问题描述

我已经定义了一个数据库模型,其中一个任务对象与同一个任务对象有关系。

class Task(models.Model):
    id = models.AutoField(primary_key=True)
    status_id = models.ForeignKey(Status,null=True,on_delete=models.SET_NULL)
    parent_id = models.ForeignKey('self',on_delete=models.SET_NULL)
    name = models.CharField(max_length=128)
    description = models.CharField(max_length=256)
    entry = models.IntegerField()

在 Graphql API 中,我可以为自己获取任务对象。我想通过调用一次对对象类型任务的调用调用 graphql 查询来实现检索相同类型的整个对象树的可能性。可能吗?

class TaskType(graphene.ObjectType):
    id = graphene.NonNull(graphene.Int)
    parent_id = graphene.Field(graphene.Int)
    name = graphene.NonNull(graphene.String)
    description = graphene.NonNull(graphene.String)
    entry = graphene.NonNull(graphene.Int)
    parent = graphene.List(lambda: TaskType)

    @staticmethod
    def resolve_parent(task,_info):
        return Task.objects.filter(parent_id=task.id)

我知道我可以以 JSONString 类型返回这样的树,但我正在寻找不同的解决方案。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)