mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 17:52:19 +03:00
Add convert_choices_to_enum meta option
This commit is contained in:
parent
6e8dce95ae
commit
010f3c217c
|
@ -52,13 +52,13 @@ def get_choices(choices):
|
||||||
yield name, value, description
|
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:
|
if registry is not None:
|
||||||
converted = registry.get_converted_field(field)
|
converted = registry.get_converted_field(field)
|
||||||
if converted:
|
if converted:
|
||||||
return converted
|
return converted
|
||||||
choices = getattr(field, "choices", None)
|
choices = getattr(field, "choices", None)
|
||||||
if choices:
|
if choices and convert_choices_to_enum:
|
||||||
meta = field.model._meta
|
meta = field.model._meta
|
||||||
name = to_camel_case("{}_{}".format(meta.object_name, field.name))
|
name = to_camel_case("{}_{}".format(meta.object_name, field.name))
|
||||||
choices = list(get_choices(choices))
|
choices = list(get_choices(choices))
|
||||||
|
|
|
@ -18,7 +18,8 @@ if six.PY3:
|
||||||
from typing import Type
|
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)
|
_model_fields = get_model_fields(model)
|
||||||
|
|
||||||
fields = OrderedDict()
|
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.
|
# in there. Or when we exclude this field in exclude_fields.
|
||||||
# Or when there is no back reference.
|
# Or when there is no back reference.
|
||||||
continue
|
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
|
fields[name] = converted
|
||||||
|
|
||||||
return fields
|
return fields
|
||||||
|
@ -63,6 +74,7 @@ class DjangoObjectType(ObjectType):
|
||||||
connection_class=None,
|
connection_class=None,
|
||||||
use_connection=None,
|
use_connection=None,
|
||||||
interfaces=(),
|
interfaces=(),
|
||||||
|
convert_choices_to_enum=True,
|
||||||
_meta=None,
|
_meta=None,
|
||||||
**options
|
**options
|
||||||
):
|
):
|
||||||
|
@ -90,7 +102,14 @@ class DjangoObjectType(ObjectType):
|
||||||
)
|
)
|
||||||
|
|
||||||
django_fields = yank_fields_from_attrs(
|
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:
|
if use_connection is None and interfaces:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user