mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-04-26 12:03:47 +03:00
Set conversion to enum
This commit is contained in:
parent
e99d22510f
commit
8e2f626f35
|
@ -86,10 +86,17 @@ def convert_form_field_to_id(field):
|
||||||
return ID(required=field.required)
|
return ID(required=field.required)
|
||||||
|
|
||||||
|
|
||||||
@convert_form_field.register(forms.TypedChoiceField)
|
def convert_form_field_with_choices(name, field):
|
||||||
def convert_form_to_enum(field, name):
|
"""
|
||||||
|
Helper method to convert a field to graphene Field type.
|
||||||
|
:param name: form field's name
|
||||||
|
:param field: form field to convert to
|
||||||
|
:return: graphene Field
|
||||||
|
"""
|
||||||
choices = getattr(field, 'choices', None)
|
choices = getattr(field, 'choices', None)
|
||||||
name = to_camel_case(name)
|
|
||||||
|
if choices:
|
||||||
|
name = to_camel_case(field.label or name)
|
||||||
choices = list(get_choices(choices))
|
choices = list(get_choices(choices))
|
||||||
named_choices = [(c[0], c[1]) for c in choices]
|
named_choices = [(c[0], c[1]) for c in choices]
|
||||||
named_choices_descriptions = {c[0]: c[2] for c in choices}
|
named_choices_descriptions = {c[0]: c[2] for c in choices}
|
||||||
|
@ -104,4 +111,5 @@ def convert_form_to_enum(field, name):
|
||||||
return named_choices_descriptions[self.name]
|
return named_choices_descriptions[self.name]
|
||||||
|
|
||||||
enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType)
|
enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType)
|
||||||
return enum(description=field.help_text, required=field.required)
|
return enum(description=field.help_text, required=field.required) # pylint: disable=E1102
|
||||||
|
return convert_form_field(field)
|
||||||
|
|
|
@ -13,7 +13,7 @@ from graphene.types.mutation import MutationOptions
|
||||||
from graphene.types.utils import yank_fields_from_attrs
|
from graphene.types.utils import yank_fields_from_attrs
|
||||||
from graphene_django.registry import get_global_registry
|
from graphene_django.registry import get_global_registry
|
||||||
|
|
||||||
from .converter import convert_form_field
|
from .converter import convert_form_field, convert_form_field_with_choices
|
||||||
from .types import ErrorType
|
from .types import ErrorType
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,11 +30,7 @@ def fields_for_form(form, only_fields, exclude_fields):
|
||||||
if is_not_in_only or is_excluded:
|
if is_not_in_only or is_excluded:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
choices = getattr(field, 'choices', None)
|
fields[name] = convert_form_field_with_choices(name, field)
|
||||||
if choices:
|
|
||||||
fields[name] = convert_form_field(field, field.label or name)
|
|
||||||
else:
|
|
||||||
fields[name] = convert_form_field(field)
|
|
||||||
return fields
|
return fields
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ from graphene import (
|
||||||
Enum,
|
Enum,
|
||||||
)
|
)
|
||||||
|
|
||||||
from ..converter import convert_form_field
|
from ..converter import convert_form_field, convert_form_field_with_choices
|
||||||
|
|
||||||
|
|
||||||
def assert_conversion(django_field, graphene_field, *args):
|
def assert_conversion(django_field, graphene_field, *args):
|
||||||
|
@ -116,6 +116,6 @@ def test_should_manytoone_convert_connectionorlist():
|
||||||
|
|
||||||
|
|
||||||
def test_should_typed_choice_convert_enum():
|
def test_should_typed_choice_convert_enum():
|
||||||
field = forms.TypedChoiceField(choices=(('A', 'Choice A'), ('B', 'Choice B')))
|
field = forms.TypedChoiceField(choices=(('A', 'Choice A'), ('B', 'Choice B')), label='field')
|
||||||
graphene_type = convert_form_field(field, 'field')
|
graphene_type = convert_form_field_with_choices('field_name', field)
|
||||||
assert isinstance(graphene_type, Enum)
|
assert isinstance(graphene_type, Enum)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user