Skip to content

Commit ecdbae3

Browse files
committed
Add custom function setting
1 parent f9252e3 commit ecdbae3

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

graphene_django/converter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ def description(self):
7070

7171

7272
def generate_enum_name(django_model_meta, field):
73-
if graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True:
73+
if graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME and callable(
74+
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME
75+
):
76+
name = graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME(field)
77+
elif graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True:
7478
name = "DjangoModel{app_label}{object_name}{field_name}Choices".format(
7579
app_label=to_camel_case(django_model_meta.app_label.title()),
7680
object_name=django_model_meta.object_name,

graphene_django/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"CAMELCASE_ERRORS": False,
3939
# Set to True to enable v3 naming convention for choice field Enum's
4040
"DJANGO_CHOICE_FIELD_ENUM_V3_NAMING": False,
41+
"DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
4142
}
4243

4344
if settings.DEBUG:

graphene_django/tests/test_types.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,14 @@ class Query(ObjectType):
514514
query: Query
515515
}
516516
517-
enum DjangoModelTestsPetModelKindChoices {
517+
enum CustomEnumKind {
518518
CAT
519519
DOG
520520
}
521521
522522
type PetModelKind {
523523
id: ID!
524-
kind: DjangoModelTestsPetModelKindChoices!
524+
kind: CustomEnumKind!
525525
}
526526
527527
type Query {
@@ -531,15 +531,13 @@ class Query(ObjectType):
531531
)
532532
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = False
533533

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-
)
534+
def test_django_objecttype_choices_custom_enum_name(self, PetModel):
535+
def custom_name(field):
536+
return f"CustomEnum{field.name.title()}"
539537

540-
class PetModelKind(DjangoObjectType):
541-
kind = Field(convert_choice(PetModel, "kind", name="CustomEnumName"))
538+
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = custom_name
542539

540+
class PetModelKind(DjangoObjectType):
543541
class Meta:
544542
model = PetModel
545543
fields = ["id", "kind"]
@@ -570,3 +568,5 @@ class Query(ObjectType):
570568
}
571569
"""
572570
)
571+
572+
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = None

0 commit comments

Comments
 (0)