diff --git a/graphene_django/converter.py b/graphene_django/converter.py index c7c5cd6..09c5583 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -31,6 +31,8 @@ singledispatch = import_single_dispatch() def convert_choice_name(name): name = force_text(name).encode('utf8').decode('ascii', 'ignore') name = to_const(name) + if name.startswith('_'): + name = "A%s" % name try: assert_valid_name(name) except AssertionError: diff --git a/graphene_django/tests/models.py b/graphene_django/tests/models.py index f54575f..39e626b 100644 --- a/graphene_django/tests/models.py +++ b/graphene_django/tests/models.py @@ -4,7 +4,7 @@ from __future__ import absolute_import from django.db import models from django.utils.translation import ugettext_lazy as _ -CHOICES = ((1, u"1: this漢"), (2, _(u"2: that漢"))) +CHOICES = ((1, u"1: this漢"), (2, _(u"2: that漢")), (3, "__amount__")) class Pet(models.Model): diff --git a/graphene_django/tests/test_converter.py b/graphene_django/tests/test_converter.py index c6ee128..466e607 100644 --- a/graphene_django/tests/test_converter.py +++ b/graphene_django/tests/test_converter.py @@ -215,6 +215,24 @@ def test_field_with_choices_collision(): convert_django_field_with_choices(field) +def test_field_with_choices_underscore(): + field = models.CharField( + choices=( + ("__amount__", "Amount"), + ("__percentage__", "Percentage"), + ), + ) + + class UnderscoreChoicesModel(models.Model): + ourfield = field + + class Meta: + app_label = "test" + + graphene_type = convert_django_field_with_choices(field) + assert len(graphene_type._meta.enum.__members__) == 2 + + def test_should_float_convert_float(): assert_conversion(models.FloatField, graphene.Float) diff --git a/graphene_django/tests/test_types.py b/graphene_django/tests/test_types.py index 888faf4..25dbfbc 100644 --- a/graphene_django/tests/test_types.py +++ b/graphene_django/tests/test_types.py @@ -174,6 +174,7 @@ type Reporter { enum ReporterAChoice { A_1_THIS A_2_THAT + A__AMOUNT__ } enum ReporterReporterType {