【发布时间】:2020-02-25 16:25:50
【问题描述】:
我有一个 django 模型,它有一个与自身相关的外键,我想将此模型表示为石墨烯 ObjectType。
我知道使用 graphene_django 库中的 DjangoObjectType 来做这件事很简单。
我正在寻找一个不使用 graphene_django 的优雅 python 解决方案。
我要代表的模型示例
# models.py
class Category(models.Model):
name = models.CharField(unique=True, max_length=200)
parent = models.ForeignKey(
'self', on_delete=models.SET_NULL, null=True, blank=True,
related_name='child_category')
下面的架构显然无法扩展,ParentCategoryType 没有parent 字段,因此严格来说它不是CategoryType 的父级。
# schema.py
class ParentCategoryType(graphene.ObjectType):
id = graphene.types.uuid.UUID()
name = graphene.String()
class CategoryType(graphene.ObjectType):
id = graphene.types.uuid.UUID()
name = graphene.String()
parent = graphene.Field(ParentCategoryType)
下面的代码给出了CategoryType未定义的错误。
#schema.py
class CategoryType(graphene.ObjectType):
id = graphene.types.uuid.UUID()
name = graphene.String()
parent = graphene.Field(CategoryType)
非常感谢任何帮助。
【问题讨论】: