Skip to content

Commit 6eb0008

Browse files
committed
Merge pull request #190 from graphql-python/fix_lazy_translated_choice
Fix lazy translated choice
2 parents 0f94f2b + edfbbf5 commit 6eb0008

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

graphene/contrib/django/converter.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.db import models
2+
from django.utils.encoding import force_text
23

34
from ...core.classtypes.enum import Enum
45
from ...core.types.custom_scalars import DateTime, JSONString
@@ -14,13 +15,14 @@
1415

1516
def convert_choices(choices):
1617
for value, name in choices:
17-
yield to_const(name), value
18+
yield to_const(force_text(name)), value
1819

1920

2021
def convert_django_field_with_choices(field):
2122
choices = getattr(field, 'choices', None)
22-
if choices:
23-
meta = field.model._meta
23+
model = getattr(field, 'model', None)
24+
if choices and model:
25+
meta = model._meta
2426
name = '{}_{}_{}'.format(meta.app_label, meta.object_name, field.name)
2527
return Enum(name.upper(), list(convert_choices(choices)), description=field.help_text)
2628
return convert_django_field(field)

graphene/contrib/django/tests/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
from __future__ import absolute_import
22

33
from django.db import models
4+
from django.utils.translation import ugettext_lazy as _
5+
6+
CHOICES = (
7+
(1, 'this'),
8+
(2, _('that'))
9+
)
410

511

612
class Pet(models.Model):
@@ -22,6 +28,7 @@ class Reporter(models.Model):
2228
last_name = models.CharField(max_length=30)
2329
email = models.EmailField()
2430
pets = models.ManyToManyField('self')
31+
a_choice = models.CharField(max_length=30, choices=CHOICES)
2532

2633
def __str__(self): # __unicode__ on Python 2
2734
return "%s %s" % (self.first_name, self.last_name)

graphene/contrib/django/tests/test_converter.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from django.db import models
3+
from django.utils.translation import ugettext_lazy as _
34
from py.test import raises
45

56
import graphene
@@ -117,6 +118,21 @@ class Meta:
117118
assert graphene_type.__enum__.__members__['ENGLISH'].value == 'en'
118119

119120

121+
def test_field_with_choices_gettext():
122+
field = models.CharField(help_text='Language', choices=(
123+
('es', _('Spanish')),
124+
('en', _('English'))
125+
))
126+
127+
class TranslatedChoicesModel(models.Model):
128+
language = field
129+
130+
class Meta:
131+
app_label = 'test'
132+
133+
convert_django_field_with_choices(field)
134+
135+
120136
def test_should_float_convert_float():
121137
assert_conversion(models.FloatField, graphene.Float)
122138

0 commit comments

Comments
 (0)