extend your graphene.Enum
instances with the class below
from inspect import getmembers, isroutine
class EnumChoices:
@classmethod
def choices(self):
attributes = getmembers(self, lambda a: not (isroutine(a)))
values = [(a[0], a[1]._value_) for a in attributes if hasattr(a[1], "_value_")]
return values
Your Example Enum will look like the one below
# import the EnumChoices class above
from graphene import Enum
class YourExampleChoices(EnumChoices, Enum):
BAD = "Bad"
MEH = "Meh"
NORMAL = "Normal"
LIKE = "Like"
LOVE = "Love"
Your example graphene.types.InputObjectType
will look like the one below
# import the YourExampleChoices class above
from graphene.types import InputObjectType
class YourExampleChoicesMutationInput(InputObjectType):
choice = YourExampleChoices(required=True)
Your example graphene.Mutation
will look like the one below
# import the YourExampleChoicesMutationInput class above
import graphene
from graphene.types.mutation import Mutation
class YourExampleMutation(Mutation):
# make sure to define your mutation results fields
class Arguments:
input = YourExampleChoicesMutationInput(required=True)
def mutate(root, info, input):
# what do you want this mutation to do?
pass
Your Django model will look like the one below
# import the YourExampleChoices class above
# notice that `YourExampleChoices.choices()` is callable, this is slightly different from the `Textchoices.choices` which isn't callable
class YourExampleModel(TimestampBase):
choices = models.CharField(
max_length=6,
choices=YourExampleChoices.choices(),
default=YourExampleChoices.NORMAL._value_
)