mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-11 12:16:58 +03:00
Merge pull request #191 from graphql-python/django-choices-grouping
Django choices grouping
This commit is contained in:
commit
de424f7d21
|
@ -15,16 +15,20 @@ singledispatch = import_single_dispatch()
|
|||
|
||||
def convert_choices(choices):
|
||||
for value, name in choices:
|
||||
yield to_const(force_text(name)), value
|
||||
if isinstance(name, (tuple, list)):
|
||||
for choice in convert_choices(name):
|
||||
yield choice
|
||||
else:
|
||||
yield to_const(force_text(name)), value
|
||||
|
||||
|
||||
def convert_django_field_with_choices(field):
|
||||
choices = getattr(field, 'choices', None)
|
||||
model = getattr(field, 'model', None)
|
||||
if choices and model:
|
||||
meta = model._meta
|
||||
if choices:
|
||||
meta = field.model._meta
|
||||
name = '{}_{}_{}'.format(meta.app_label, meta.object_name, field.name)
|
||||
return Enum(name.upper(), list(convert_choices(choices)), description=field.help_text)
|
||||
graphql_choices = list(convert_choices(choices))
|
||||
return Enum(name.upper(), graphql_choices, description=field.help_text)
|
||||
return convert_django_field(field)
|
||||
|
||||
|
||||
|
|
|
@ -118,6 +118,23 @@ def test_field_with_choices_convert_enum():
|
|||
assert graphene_type.__enum__.__members__['ENGLISH'].value == 'en'
|
||||
|
||||
|
||||
def test_field_with_grouped_choices():
|
||||
field = models.CharField(help_text='Language', choices=(
|
||||
('Europe', (
|
||||
('es', 'Spanish'),
|
||||
('en', 'English'),
|
||||
)),
|
||||
))
|
||||
|
||||
class GroupedChoicesModel(models.Model):
|
||||
language = field
|
||||
|
||||
class Meta:
|
||||
app_label = 'test'
|
||||
|
||||
convert_django_field_with_choices(field)
|
||||
|
||||
|
||||
def test_field_with_choices_gettext():
|
||||
field = models.CharField(help_text='Language', choices=(
|
||||
('es', _('Spanish')),
|
||||
|
|
Loading…
Reference in New Issue
Block a user