File tree Expand file tree Collapse file tree 2 files changed +49
-2
lines changed Expand file tree Collapse file tree 2 files changed +49
-2
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,13 @@ def generate_enum_name(django_model_meta, field):
81
81
return name
82
82
83
83
84
+ def convert_choice_field_to_enum (field , name = None ):
85
+ if name is None :
86
+ name = generate_enum_name (field .model ._meta , field )
87
+ choices = field .choices
88
+ return convert_choices_to_named_enum_with_descriptions (name , choices )
89
+
90
+
84
91
def convert_django_field_with_choices (
85
92
field , registry = None , convert_choices_to_enum = True
86
93
):
@@ -90,8 +97,7 @@ def convert_django_field_with_choices(
90
97
return converted
91
98
choices = getattr (field , "choices" , None )
92
99
if choices and convert_choices_to_enum :
93
- name = generate_enum_name (field .model ._meta , field )
94
- enum = convert_choices_to_named_enum_with_descriptions (name , choices )
100
+ enum = convert_choice_field_to_enum (field )
95
101
required = not (field .blank or field .null )
96
102
converted = enum (description = field .help_text , required = required )
97
103
else :
Original file line number Diff line number Diff line change 11
11
from .. import registry
12
12
from ..settings import graphene_settings
13
13
from ..types import DjangoObjectType , DjangoObjectTypeOptions
14
+ from ..converter import convert_choice_field_to_enum
14
15
from .models import Article as ArticleModel
15
16
from .models import Reporter as ReporterModel
16
17
@@ -529,3 +530,43 @@ class Query(ObjectType):
529
530
"""
530
531
)
531
532
graphene_settings .CHOICES_TO_ENUM_UNIQUE_TYPE_NAME = False
533
+
534
+ def test_django_objecttype_choices_override_enum (self , PetModel ):
535
+ def convert_choice (model , field_name , ** kwargs ):
536
+ return convert_choice_field_to_enum (
537
+ model ._meta .get_field (field_name ), ** kwargs
538
+ )
539
+
540
+ class PetModelKind (DjangoObjectType ):
541
+ kind = Field (convert_choice (PetModel , "kind" , name = "CustomEnumName" ))
542
+
543
+ class Meta :
544
+ model = PetModel
545
+ fields = ["id" , "kind" ]
546
+
547
+ class Query (ObjectType ):
548
+ pet = Field (PetModelKind )
549
+
550
+ schema = Schema (query = Query )
551
+
552
+ assert str (schema ) == dedent (
553
+ """\
554
+ schema {
555
+ query: Query
556
+ }
557
+
558
+ enum DjangoModelTestsPetModelKindChoices {
559
+ CAT
560
+ DOG
561
+ }
562
+
563
+ type PetModelKind {
564
+ id: ID!
565
+ kind: DjangoModelTestsPetModelKindChoices!
566
+ }
567
+
568
+ type Query {
569
+ pet: PetModelKind
570
+ }
571
+ """
572
+ )
You can’t perform that action at this time.
0 commit comments