From 010f3c217cad2c4f8a42bf317a5be32492873684 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Thu, 13 Jun 2019 17:19:08 +0100 Subject: [PATCH] Add convert_choices_to_enum meta option --- graphene_django/converter.py | 4 ++-- graphene_django/types.py | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/graphene_django/converter.py b/graphene_django/converter.py index 1bb16f4..11a427e 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -52,13 +52,13 @@ def get_choices(choices): yield name, value, description -def convert_django_field_with_choices(field, registry=None): +def convert_django_field_with_choices(field, registry=None, convert_choices_to_enum=True): if registry is not None: converted = registry.get_converted_field(field) if converted: return converted choices = getattr(field, "choices", None) - if choices: + if choices and convert_choices_to_enum: meta = field.model._meta name = to_camel_case("{}_{}".format(meta.object_name, field.name)) choices = list(get_choices(choices)) diff --git a/graphene_django/types.py b/graphene_django/types.py index a1e17b3..9c38bd3 100644 --- a/graphene_django/types.py +++ b/graphene_django/types.py @@ -18,7 +18,8 @@ if six.PY3: from typing import Type -def construct_fields(model, registry, only_fields, exclude_fields): +def construct_fields(model, registry, only_fields, exclude_fields, + convert_choices_to_enum): _model_fields = get_model_fields(model) fields = OrderedDict() @@ -33,7 +34,17 @@ def construct_fields(model, registry, only_fields, exclude_fields): # in there. Or when we exclude this field in exclude_fields. # Or when there is no back reference. continue - converted = convert_django_field_with_choices(field, registry) + + _convert_choices_to_enum = convert_choices_to_enum + if not isinstance(_convert_choices_to_enum, bool): + # then `convert_choices_to_enum` is a list of field names to convert + if name in _convert_choices_to_enum: + _convert_choices_to_enum = True + else: + _convert_choices_to_enum = False + + converted = convert_django_field_with_choices( + field, registry, convert_choices_to_enum=_convert_choices_to_enum) fields[name] = converted return fields @@ -63,6 +74,7 @@ class DjangoObjectType(ObjectType): connection_class=None, use_connection=None, interfaces=(), + convert_choices_to_enum=True, _meta=None, **options ): @@ -90,7 +102,14 @@ class DjangoObjectType(ObjectType): ) django_fields = yank_fields_from_attrs( - construct_fields(model, registry, only_fields, exclude_fields), _as=Field + construct_fields( + model, + registry, + only_fields, + exclude_fields, + convert_choices_to_enum + ), + _as=Field ) if use_connection is None and interfaces: