Skip to content

Commit 5661db8

Browse files
authored
Merge pull request #156 from aaxelb/master
Fix #87: Don't create duplicate Enums for fields with choices
2 parents 14dca82 + b5e7614 commit 5661db8

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

graphene_django/converter.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def get_choices(choices):
4040

4141

4242
def convert_django_field_with_choices(field, registry=None):
43+
if registry is not None:
44+
converted = registry.get_converted_field(field)
45+
if converted:
46+
return converted
4347
choices = getattr(field, 'choices', None)
4448
if choices:
4549
meta = field.model._meta
@@ -55,8 +59,12 @@ def description(self):
5559
return named_choices_descriptions[self.name]
5660

5761
enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType)
58-
return enum(description=field.help_text, required=not field.null)
59-
return convert_django_field(field, registry)
62+
converted = enum(description=field.help_text, required=not field.null)
63+
else:
64+
converted = convert_django_field(field, registry)
65+
if registry is not None:
66+
registry.register_converted_field(field, converted)
67+
return converted
6068

6169

6270
@singledispatch

graphene_django/registry.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Registry(object):
33

44
def __init__(self):
55
self._registry = {}
6-
self._registry_models = {}
6+
self._field_registry = {}
77

88
def register(self, cls):
99
from .types import DjangoObjectType
@@ -20,6 +20,12 @@ def register(self, cls):
2020
def get_type_for_model(self, model):
2121
return self._registry.get(model)
2222

23+
def register_converted_field(self, field, converted):
24+
self._field_registry[field] = converted
25+
26+
def get_converted_field(self, field):
27+
return self._field_registry.get(field)
28+
2329

2430
registry = None
2531

graphene_django/tests/test_query.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,3 +748,35 @@ class Query(graphene.ObjectType):
748748
result = schema.execute(query)
749749
assert not result.errors
750750
assert result.data == expected
751+
752+
753+
def test_should_handle_inherited_choices():
754+
class BaseModel(models.Model):
755+
choice_field = models.IntegerField(choices=((0, 'zero'), (1, 'one')))
756+
757+
class ChildModel(BaseModel):
758+
class Meta:
759+
proxy = True
760+
761+
class BaseType(DjangoObjectType):
762+
class Meta:
763+
model = BaseModel
764+
765+
class ChildType(DjangoObjectType):
766+
class Meta:
767+
model = ChildModel
768+
769+
class Query(graphene.ObjectType):
770+
base = graphene.Field(BaseType)
771+
child = graphene.Field(ChildType)
772+
773+
schema = graphene.Schema(query=Query)
774+
query = '''
775+
query {
776+
child {
777+
choiceField
778+
}
779+
}
780+
'''
781+
result = schema.execute(query)
782+
assert not result.errors

0 commit comments

Comments
 (0)