From 5ad21f17656ecdd0b79219855c1e767ecd77b19b Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Thu, 13 Oct 2016 22:01:01 -0400 Subject: [PATCH] Fix #19 by using choice keys as enum keys, not choice descriptions --- graphene_django/converter.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/graphene_django/converter.py b/graphene_django/converter.py index 9fcc3c5..4ae1e4e 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -7,6 +7,7 @@ from graphene.relay import is_node from graphene.types.datetime import DateTime from graphene.types.json import JSONString from graphene.utils.str_converters import to_const +from graphql import assert_valid_name from .compat import (ArrayField, HStoreField, JSONField, RangeField, RelatedObject, UUIDField) @@ -17,7 +18,12 @@ singledispatch = import_single_dispatch() def convert_choice_name(name): - return to_const(force_text(name)) + name = to_const(force_text(name)) + try: + assert_valid_name(name) + except AssertionError: + name = "A_%s" % name + return name def get_choices(choices): @@ -26,7 +32,7 @@ def get_choices(choices): for choice in get_choices(help_text): yield choice else: - name = convert_choice_name(help_text) + name = convert_choice_name(value) description = help_text yield name, value, description