Skip to content

Commit 7aa7c5a

Browse files
committed
Added support for mapping Django Fields with choices to Enum types. Fixed #95
1 parent 407b9ea commit 7aa7c5a

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

graphene/contrib/django/converter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
from django.db import models
22

33
from ...core.types.scalars import ID, Boolean, Float, Int, String
4+
from ...core.classtypes.enum import Enum
45
from .compat import RelatedObject, UUIDField
56
from .utils import get_related_model, import_single_dispatch
67

78
singledispatch = import_single_dispatch()
89

910

11+
def convert_django_field_with_choices(field):
12+
choices = getattr(field, 'choices', None)
13+
if choices:
14+
meta = field.model._meta
15+
name = '{}_{}_{}'.format(meta.app_label, meta.object_name, field.name)
16+
return Enum(name.upper(), choices, description=field.help_text)
17+
return convert_django_field(field)
18+
19+
1020
@singledispatch
1121
def convert_django_field(field):
1222
raise Exception(

graphene/contrib/django/tests/test_converter.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from py.test import raises
33

44
import graphene
5-
from graphene.contrib.django.converter import convert_django_field
5+
from graphene.contrib.django.converter import (
6+
convert_django_field, convert_django_field_with_choices)
67
from graphene.contrib.django.fields import (ConnectionOrListField,
78
DjangoModelField)
89

@@ -86,6 +87,26 @@ def test_should_nullboolean_convert_boolean():
8687
assert field.required is False
8788

8889

90+
def test_field_with_choices_convert_enum():
91+
field = models.CharField(help_text='Language', choices=(
92+
('es', 'Spanish'),
93+
('en', 'English')
94+
))
95+
96+
class TranslatedModel(models.Model):
97+
language = field
98+
99+
class Meta:
100+
app_label = 'test'
101+
102+
graphene_type = convert_django_field_with_choices(field)
103+
assert issubclass(graphene_type, graphene.Enum)
104+
assert graphene_type._meta.type_name == 'TEST_TRANSLATEDMODEL_LANGUAGE'
105+
assert graphene_type._meta.description == 'Language'
106+
assert graphene_type.__enum__.__members__['es'].value == 'Spanish'
107+
assert graphene_type.__enum__.__members__['en'].value == 'English'
108+
109+
89110
def test_should_float_convert_float():
90111
assert_conversion(models.FloatField, graphene.Float)
91112

graphene/contrib/django/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from ...core.classtypes.objecttype import ObjectType, ObjectTypeMeta
77
from ...relay.types import Connection, Node, NodeMeta
8-
from .converter import convert_django_field
8+
from .converter import convert_django_field_with_choices
99
from .options import DjangoOptions
1010
from .utils import get_reverse_fields
1111

@@ -29,7 +29,7 @@ def construct_fields(cls):
2929
# We skip this field if we specify only_fields and is not
3030
# in there. Or when we exclude this field in exclude_fields
3131
continue
32-
converted_field = convert_django_field(field)
32+
converted_field = convert_django_field_with_choices(field)
3333
cls.add_to_class(field.name, converted_field)
3434

3535
def construct(cls, *args, **kwargs):

0 commit comments

Comments
 (0)