File tree Expand file tree Collapse file tree 3 files changed +28
-3
lines changed Expand file tree Collapse file tree 3 files changed +28
-3
lines changed Original file line number Diff line number Diff line change 1
1
from django .db import models
2
+ from django .utils .encoding import force_text
2
3
3
4
from ...core .classtypes .enum import Enum
4
5
from ...core .types .custom_scalars import DateTime , JSONString
14
15
15
16
def convert_choices (choices ):
16
17
for value , name in choices :
17
- yield to_const (name ), value
18
+ yield to_const (force_text ( name ) ), value
18
19
19
20
20
21
def convert_django_field_with_choices (field ):
21
22
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
24
26
name = '{}_{}_{}' .format (meta .app_label , meta .object_name , field .name )
25
27
return Enum (name .upper (), list (convert_choices (choices )), description = field .help_text )
26
28
return convert_django_field (field )
Original file line number Diff line number Diff line change 1
1
from __future__ import absolute_import
2
2
3
3
from django .db import models
4
+ from django .utils .translation import ugettext_lazy as _
5
+
6
+ CHOICES = (
7
+ (1 , 'this' ),
8
+ (2 , _ ('that' ))
9
+ )
4
10
5
11
6
12
class Pet (models .Model ):
@@ -22,6 +28,7 @@ class Reporter(models.Model):
22
28
last_name = models .CharField (max_length = 30 )
23
29
email = models .EmailField ()
24
30
pets = models .ManyToManyField ('self' )
31
+ a_choice = models .CharField (max_length = 30 , choices = CHOICES )
25
32
26
33
def __str__ (self ): # __unicode__ on Python 2
27
34
return "%s %s" % (self .first_name , self .last_name )
Original file line number Diff line number Diff line change 1
1
import pytest
2
2
from django .db import models
3
+ from django .utils .translation import ugettext_lazy as _
3
4
from py .test import raises
4
5
5
6
import graphene
@@ -117,6 +118,21 @@ class Meta:
117
118
assert graphene_type .__enum__ .__members__ ['ENGLISH' ].value == 'en'
118
119
119
120
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
+
120
136
def test_should_float_convert_float ():
121
137
assert_conversion (models .FloatField , graphene .Float )
122
138
You can’t perform that action at this time.
0 commit comments