Skip to content

Fix #87: Don't create duplicate Enums for fields with choices #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def get_choices(choices):


def convert_django_field_with_choices(field, registry=None):
if registry is not None:
converted = registry.get_converted_field(field)
if converted:
return converted
choices = getattr(field, 'choices', None)
if choices:
meta = field.model._meta
Expand All @@ -56,8 +60,12 @@ def description(self):
return named_choices_descriptions[self.name]

enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType)
return enum(description=field.help_text, required=not field.null)
return convert_django_field(field, registry)
converted = enum(description=field.help_text, required=not field.null)
else:
converted = convert_django_field(field, registry)
if registry is not None:
registry.register_converted_field(field, converted)
return converted


@singledispatch
Expand Down
8 changes: 7 additions & 1 deletion graphene_django/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Registry(object):

def __init__(self):
self._registry = {}
self._registry_models = {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_registry_models is not referenced anywhere, so it seemed safe to remove.

self._field_registry = {}

def register(self, cls):
from .types import DjangoObjectType
Expand All @@ -19,6 +19,12 @@ def register(self, cls):
def get_type_for_model(self, model):
return self._registry.get(model)

def register_converted_field(self, field, converted):
self._field_registry[field] = converted

def get_converted_field(self, field):
return self._field_registry.get(field)


registry = None

Expand Down
32 changes: 32 additions & 0 deletions graphene_django/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,35 @@ class Query(graphene.ObjectType):
result = schema.execute(query)
assert not result.errors
assert result.data == expected


def test_should_handle_inherited_choices():
class BaseModel(models.Model):
choice_field = models.IntegerField(choices=((0, 'zero'), (1, 'one')))

class ChildModel(BaseModel):
class Meta:
proxy = True

class BaseType(DjangoObjectType):
class Meta:
model = BaseModel

class ChildType(DjangoObjectType):
class Meta:
model = ChildModel

class Query(graphene.ObjectType):
base = graphene.Field(BaseType)
child = graphene.Field(ChildType)

schema = graphene.Schema(query=Query)
query = '''
query {
child {
choiceField
}
}
'''
result = schema.execute(query)
assert not result.errors