有没有办法让石墨烯与 django GenericRelation 字段一起使用?

Vin*_*uki 4 python django graphql graphene-python

我有一些 django 模型通用关系字段,我希望它们出现在 graphql 查询中。石墨烯支持通用类型吗?

class Attachment(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    file = models.FileField(upload_to=user_directory_path)
Run Code Online (Sandbox Code Playgroud)
class Aparto(models.Model):
    agency = models.CharField(max_length=100, default='Default')
    features = models.TextField()
    attachments = GenericRelation(Attachment)
Run Code Online (Sandbox Code Playgroud)

石墨烯类:

class ApartoType(DjangoObjectType):
    class Meta:
        model = Aparto
Run Code Online (Sandbox Code Playgroud)
class Query(graphene.ObjectType):
    all  = graphene.List(ApartoType)
    def resolve_all(self, info, **kwargs):
        return Aparto.objects.all()

schema = graphene.Schema(query=Query)
Run Code Online (Sandbox Code Playgroud)

我希望附件字段出现在 graphql 查询结果中。仅显示代理和功能。

Chr*_*son 5

您需要公开Attachment您的架构。石墨烯需要type与任何相关领域合作,因此它们也需要被暴露。

此外,您可能需要解析相关的attachments,因此您需要为它们添加一个解析器。

在你的石墨烯课程中,尝试:

class AttachmentType(DjangoObjectType):
    class Meta:
        model = Attachment

class ApartoType(DjangoObjectType):
    class Meta:
        model = Aparto

    attachments = graphene.List(AttachmentType)
    def resolve_attachments(root, info):
        return root.attachments.all()
Run Code Online (Sandbox Code Playgroud)