File tree Expand file tree Collapse file tree 2 files changed +26
-5
lines changed Expand file tree Collapse file tree 2 files changed +26
-5
lines changed Original file line number Diff line number Diff line change 15
15
16
16
def convert_choices (choices ):
17
17
for value , name in choices :
18
- yield to_const (force_text (name )), value
18
+ if isinstance (name , (tuple , list )):
19
+ for choice in convert_choices (name ):
20
+ yield choice
21
+ else :
22
+ yield to_const (force_text (name )), value
19
23
20
24
21
25
def convert_django_field_with_choices (field ):
22
26
choices = getattr (field , 'choices' , None )
23
- model = getattr (field , 'model' , None )
24
- if choices and model :
25
- meta = model ._meta
27
+ if choices :
28
+ meta = field .model ._meta
26
29
name = '{}_{}_{}' .format (meta .app_label , meta .object_name , field .name )
27
- return Enum (name .upper (), list (convert_choices (choices )), description = field .help_text )
30
+ graphql_choices = list (convert_choices (choices ))
31
+ return Enum (name .upper (), graphql_choices , description = field .help_text )
28
32
return convert_django_field (field )
29
33
30
34
Original file line number Diff line number Diff line change @@ -118,6 +118,23 @@ class Meta:
118
118
assert graphene_type .__enum__ .__members__ ['ENGLISH' ].value == 'en'
119
119
120
120
121
+ def test_field_with_grouped_choices ():
122
+ field = models .CharField (help_text = 'Language' , choices = (
123
+ ('Europe' , (
124
+ ('es' , 'Spanish' ),
125
+ ('en' , 'English' ),
126
+ )),
127
+ ))
128
+
129
+ class GroupedChoicesModel (models .Model ):
130
+ language = field
131
+
132
+ class Meta :
133
+ app_label = 'test'
134
+
135
+ convert_django_field_with_choices (field )
136
+
137
+
121
138
def test_field_with_choices_gettext ():
122
139
field = models .CharField (help_text = 'Language' , choices = (
123
140
('es' , _ ('Spanish' )),
You can’t perform that action at this time.
0 commit comments