Skip to content

Commit dc5799e

Browse files
committed
detect if value is a string for enum conversion else use help text to generate name
1 parent b0cba39 commit dc5799e

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

graphene_django/converter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ def convert_choice_name(name):
3838

3939

4040
def get_choices(choices):
41-
converted_names = []
41+
converted_names = set()
4242
for value, help_text in choices:
4343
if isinstance(help_text, (tuple, list)):
4444
for choice in get_choices(help_text):
4545
yield choice
4646
else:
47-
name = convert_choice_name(value)
47+
if isinstance(value, str):
48+
name = convert_choice_name(value)
49+
else:
50+
name = convert_choice_name(help_text)
4851
while name in converted_names:
4952
name += "_" + str(len(converted_names))
50-
converted_names.append(name)
53+
converted_names.add(name)
5154
description = help_text
5255
yield name, value, description
5356

graphene_django/tests/test_converter.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,25 @@ class Meta:
177177
convert_django_field_with_choices(field)
178178

179179

180+
def test_field_with_integer_choices():
181+
field = models.IntegerField(
182+
help_text="Language", choices=((1, "Spanish"), (2, "English"))
183+
)
184+
185+
class TranslatedChoicesModel(models.Model):
186+
language = field
187+
188+
class Meta:
189+
app_label = "test"
190+
191+
graphene_type = convert_django_field_with_choices(field)
192+
#assert False, str(graphene_type._meta.enum.__members__)
193+
assert graphene_type._meta.enum.__members__["SPANISH"].value == 1
194+
assert graphene_type._meta.enum.__members__["SPANISH"].description == "Spanish"
195+
assert graphene_type._meta.enum.__members__["ENGLISH"].value == 2
196+
assert graphene_type._meta.enum.__members__["ENGLISH"].description == "English"
197+
198+
180199
def test_field_with_choices_collision():
181200
field = models.CharField(
182201
help_text="Timezone",

graphene_django/tests/test_types.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ def test_schema_representation():
136136
}
137137
138138
enum ArticleImportance {
139-
A_1
140-
A_2
139+
VERY_IMPORTANT
140+
NOT_AS_IMPORTANT
141141
}
142142
143143
enum ArticleLang {
@@ -172,13 +172,13 @@ def test_schema_representation():
172172
}
173173
174174
enum ReporterAChoice {
175-
A_1
176-
A_2
175+
THIS
176+
THAT
177177
}
178178
179179
enum ReporterReporterType {
180-
A_1
181-
A_2
180+
REGULAR
181+
CNN_REPORTER
182182
}
183183
184184
type RootQuery {

0 commit comments

Comments
 (0)