Skip to content

Commit 9cff365

Browse files
committed
Import function through import_string function
1 parent d0e3a04 commit 9cff365

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

docs/settings.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,20 @@ Default: ``False``
153153
``DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME``
154154
--------------------------------------
155155

156-
Set to a function that takes the Django choice field and returns a string to completely customise the naming for the Enum type.
156+
Define the path of a function that takes the Django choice field and returns a string to completely customise the naming for the Enum type.
157157

158158
If set to a function then the ``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING`` setting is ignored.
159159

160160
Default: ``None``
161161

162162
.. code:: python
163163
164+
# myapp.utils
164165
def enum_naming(field):
165166
if isinstance(field.model, User):
166167
return f"CustomUserEnum{field.name.title()}"
167168
return f"CustomEnum{field.name.title()}"
168169
169170
GRAPHENE = {
170-
'DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME': enum_naming
171+
'DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME': "myapp.utils.enum_naming"
171172
}

graphene_django/converter.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import OrderedDict
22
from django.db import models
33
from django.utils.encoding import force_str
4+
from django.utils.module_loading import import_string
45

56
from graphene import (
67
ID,
@@ -70,10 +71,12 @@ def description(self):
7071

7172

7273
def generate_enum_name(django_model_meta, field):
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)
74+
if graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME:
75+
# Try and import custom function
76+
custom_func = import_string(
77+
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME
78+
)
79+
name = custom_func(field)
7780
elif graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True:
7881
name = "{app_label}{object_name}{field_name}Choices".format(
7982
app_label=to_camel_case(django_model_meta.app_label.title()),

graphene_django/tests/test_types.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ class Meta:
388388
assert len(record) == 0
389389

390390

391+
def custom_enum_name(field):
392+
return "CustomEnum{}".format(field.name.title())
393+
394+
391395
class TestDjangoObjectType:
392396
@pytest.fixture
393397
def PetModel(self):
@@ -532,10 +536,9 @@ class Query(ObjectType):
532536
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = False
533537

534538
def test_django_objecttype_choices_custom_enum_name(self, PetModel):
535-
def custom_name(field):
536-
return "CustomEnum{}".format(field.name.title())
537-
538-
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = custom_name
539+
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = (
540+
"graphene_django.tests.test_types.custom_enum_name"
541+
)
539542

540543
class PetModelKind(DjangoObjectType):
541544
class Meta:
@@ -553,14 +556,14 @@ class Query(ObjectType):
553556
query: Query
554557
}
555558
556-
enum DjangoModelTestsPetModelKindChoices {
559+
enum CustomEnumKind {
557560
CAT
558561
DOG
559562
}
560563
561564
type PetModelKind {
562565
id: ID!
563-
kind: DjangoModelTestsPetModelKindChoices!
566+
kind: CustomEnumKind!
564567
}
565568
566569
type Query {

0 commit comments

Comments
 (0)