如何将Django抽象类与grapheneDjango一起使用?

2024-09-30 04:26:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为两个相似的具体类提供一个唯一的接口,它们继承自一个公共抽象类

My django模型类:

class Metadata(models.Model):
    name = models.CharField(max_length=255)
    sequence = models.PositiveSmallIntegerField()
    is_choices = False

    class Meta:
        abstract = True


class MetadataScalar(Metadata):
    string_format = models.CharField(max_length=255, blank=True, null=True)


class MetadataChoices(Metadata):
    is_choices = True
    choices = models.CharField(max_length=255, blank=True, null=True)

我的graphene django api:

class MetadataNode(DjangoObjectType):
    class Meta:
        interfaces = (Node,)
        connection_class = Connection
        model = Metadata
        fields = '__all__'


class MetadataScalarNode(MetadataNode):
    class Meta:
        interfaces = (Node,)
        connection_class = Connection
        model = MetadataScalar
        fields = '__all__'


class MetadataChoicesNode(MetadataNode):
    class Meta:
        interfaces = (Node,)
        connection_class = Connection
        model = MetadataChoices
        fields = '__all__'


class CreateMetadata(ClientIDMutation):
    metadata = Field(MetadataNode)

    class Input:
        name = String(max_length=255, required=True)
        sequence = Int(required=True)
        string_format = String()
        choices = List(String)

    @classmethod
    def mutate_and_get_payload(cls, root, info, **input):
        if 'string_format' in input:
            metadata = MetadataScalar.objects.create(
                name=input.get('name'),
                sequence=input.get('sequence'),
                string_format=input.get('string_format')
            )
        elif 'choices' in input:
            metadata = MetadataChoices.objects.create(
                name=input.get('name'),
                sequence=input.get('sequence'),
                choices=','.join(input.get('choices'))
            )
        return CreateMetadata(metadata=metadata)

查询与CreateMetadata对应的graphql变异时,元数据具体类被成功创建。;-)

问题是,当查询在结果中请求创建的具体Metadata(此处为MetadataScalarMetadataChoices)时,graphql无法找到具体类的节点,并输出以下错误消息:

Expected value of type "MetadataNode" but got: MetadataScalar.

请参考以下一个查询示例:

mutation {
  createMetadata (input: {
    stringFormat: "foo"
    sequence: 12
    name: "bar"
  }) {
    metadata {
      name
      sequence
    }
  }
}

如何使其工作良好,而不必在查询的第二部分中指定两种不同的结果类型(metadataScalarmetadataChoices变量)


Tags: nametrueformatinputgetstringmodelsmax
2条回答

试试看

... on metadata{
  name
  sequence
}

使用你的界面。 Union不能有任何字段,因此如果使用Union vs接口,则需要有重复字段。 https://docs.graphene-python.org/en/latest/types/unions/

可以使用Union来指定多个不同的结果类

就你而言,那将是:

class MetadataScalarNode(DjangoObjectType):
    class Meta:
        interfaces = (Node,)
        connection_class = Connection
        model = MetadataScalar
        fields = '__all__'


class MetadataChoicesNode(DjangoObjectType):
    class Meta:
        interfaces = (Node,)
        connection_class = Connection
        model = MetadataChoices
        fields = '__all__'


class MetadataNode(Union):
    class Meta:
        types = (MetadataScalarNode, MetadataChoicesNode)

graphql查询将如下所示:

mutation {
  createMetadata (input: {
    stringFormat: "foo"
    sequence: 12
    name: "bar"
  }) {
    metadata {
      __typename
      ... on MetadataScalarNode {
        name
        sequence
        stringFormat
      }
      ... on MetadataChoicesNode {
        name
        sequence
        choices
      }
    }
  }
}

相关问题 更多 >

    热门问题