Overwrite django choices output in graphene

You can set convert_choices_to_enum to False in your Graphene-Django model which will leave them as integers.

class FooType(DjangoObjectType):
    class Meta:
        model = Foo
        convert_choices_to_enum = False

There is more information on the setting here.


The code comments say

GraphQL serializes Enum values as strings, however internally Enums can be represented by any kind of type, often integers.

So for your particular case, you're not going to be able to replace the over-the-wire values with integers easily. But it may not matter if the actual value represented by the strings ("A_1") is still an integer internally and on the client end (from the field's description values.)

In general though you can replace the automatically generated field for the field with choices by defining an enum class and adding to the definition of the DjangoObjectType. Here's an example using the documentation Enum example...

class Episode(graphene.Enum):
    NEWHOPE = 4
    EMPIRE = 5
    JEDI = 6

    @property
    def description(self):
        if self == Episode.NEWHOPE:
            return 'New Hope Episode'
        return 'Other episode'

which you could then add to your DjangoObjectType like

class FooType(DjangoObjectType):
    score = Episode()
    class Meta:
        model = Foo

Or if you want to get extra fancy you can generate the Enum field dynamically from your field's choices in Foo._meta.get_field('score').choices. See graphene_django.converter.convert_django_field_with_choices.