Chr*_*r Z 5 python django enums graphql graphene-python
我在尝试将枚举与Django和Graphene结合使用时遇到一些问题。即使枚举的值保留在SQLite中并且可以正确检索,它仍然导致错误。下面是错误的示例。
{
  "message": "Cannot return null for non-nullable field DjangoObject.vanillaEnum.",
  "locations": [
    {
      "line": 10,
      "column": 5
    }
  ]
},
我正在将Django 2.0.3和Graphene 2.0.1与Anaconda3 5.0.0和Python 3.6.4一起使用。我设法使用一个简单的示例重现了该错误,该示例在我的GitHub帐户上可用。
在中models.py,我定义了Enum使用该枚举的Python 和Django模型。此处的模型按预期工作(AFAIK),不涉及任何石墨烯依赖项。默认的SQLite数据库似乎也具有正确的值。
models.py
import enum
from django.db import models
@enum.unique
class VanillaEnum(enum.Enum):
    RED = enum.auto()
    BLUE = enum.auto()
    GREEN = enum.auto()
    @classmethod
    def choices(cls):
        return tuple((x, x) for x in cls)
class DjangoModel(models.Model):
    name = models.CharField(max_length=20)
    vanilla_enum = models.CharField(choices=VanillaEnum.choices(), default=VanillaEnum.GREEN, max_length=20)
    def __str__(self):
        return f'name={self.name}, vanilla_enun={self.vanilla_enum}'
接下来,在中schema.py,我定义了两个枚举。一种使用graphene.Enum.from_enum可以将其转换VanillaEnum为石墨烯可以使用的。第二个是使用的枚举graphene.Enum。第二个枚举是我的控制案例,它应该起作用。但是它仍然导致与上述相同的错误。
然后,我定义了两个对象,一个使用两个枚举作为字段的本地Graphene对象,以及一个映射到模型的Django-Graphene对象。
schema.py
import graphene
from graphene_django import DjangoObjectType
from djangographeneenum.models import DjangoModel, VanillaEnum
ConvertedEnum = graphene.Enum.from_enum(VanillaEnum)
class GrapheneEnum(graphene.Enum):
    RED = 1
    BLUE = 2
    GREEN = 3
class GrapheneObject(graphene.ObjectType):
    name = graphene.String()
    converted_enum = graphene.Field(ConvertedEnum)
    graphene_enum = graphene.Field(GrapheneEnum)
    def __str__(self):
        return f'name={self.name}, converted_enum={self.converted_enum}, graphene_enum={self.graphene_enum}'
class DjangoObject(DjangoObjectType):
    class Meta:
        model = DjangoModel
class Query(graphene.ObjectType):
    graphene_object = graphene.Field(GrapheneObject)
    django_model = graphene.List(DjangoObject)
    def resolve_graphene_object(self, info):
        graphene_object = GrapheneObject(name='Abc', converted_enum=ConvertedEnum.RED, graphene_enum=GrapheneEnum.BLUE)
        print(f'graphene_object: {graphene_object}')
        return graphene_object
    def resolve_django_model(self, info):
        django_model = DjangoModel.objects.get_or_create(name='RED Model', vanilla_enum=VanillaEnum.RED)
        print(f'django_model: {django_model}')
        django_model = DjangoModel.objects.get_or_create(name='BLUE Model', vanilla_enum=VanillaEnum.BLUE)
        print(f'django_model: {django_model}')
        django_model = DjangoModel.objects.get_or_create(name='GREEN Model', vanilla_enum=VanillaEnum.GREEN)
        print(f'django_model: {django_model}')
        objects_all = DjangoModel.objects.all()
        for x in objects_all:
            print(f'django_model: {x}')
        return objects_all
以下是部分输出。原生Graphene对象中的枚举都显示为null。对于映射的Django模型,整个对象是null,而不仅仅是枚举字段。
  "data": {
    "grapheneObject": {
      "name": "Abc",
      "convertedEnum": null,
      "grapheneEnum": null
    },
    "djangoModel": [
      null,
      null,
      null,
      null
    ]
  }
那么,我想念什么?
| 归档时间: | 
 | 
| 查看次数: | 1316 次 | 
| 最近记录: |