简体   繁体   English

Graphene-django - 使用枚举改变类型

[英]Graphene-django - Mutating types with enums

So, I have the following model:所以,我有以下 model:

class Semester(models.Model):
    course = models.ManyToManyField(Course, through='CourseSemester')

    class SemesterType(models.TextChoices):
        A = 'A', 'Winter'
        B = 'B', 'Spring'
        SUMMER = 'SU', 'Summer'

    name = models.CharField(
        max_length=200,
        choices=SemesterType.choices,
        default=SemesterType.A,
    )
    year = models.IntegerField()

I try to add a mutation to add a new semester.我尝试添加一个突变来添加一个新学期。 Graphene-django seems to automatically generate an Enum field for me, but how can I get it inside the arguments? Graphene-django 似乎会自动为我生成一个 Enum 字段,但我如何在 arguments 中获取它?

According to the github issues, something like SemesterType._meta.fields['name'] should work, but I can't get it right, even with wrapping it inside graphene.Argument .根据 github 问题,像SemesterType._meta.fields['name']这样的东西应该可以工作,但我做不对,即使将它包装在graphene.Argument中也是如此。

It is possible to tell Graphene not to convert it to Enum, however I'd rather avoid that if possible.可以告诉 Graphene 不要将其转换为枚举,但我宁愿尽可能避免这种情况。

Any clue how to get that right?任何线索如何做到这一点?

You should add convert_choices_to_enum = False to your DjangoObjectType您应该将convert_choices_to_enum = False添加到您的DjangoObjectType

More information is in documentation in Choices to Enum conversion section更多信息在Choices to Enum conversion 部分的文档中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM